﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using CSKernelClient;
using CSReportDll;
using CSReportGlobals;

namespace CSReportEditor
{
    public class cEditor
    {
        private fMain m_fmain = null;
        private Panel m_editor = null;
        private PictureBox m_picRule = null;
        private PictureBox m_picReport = null;
        private String m_reportFullPath = "";

        public cEditor(fMain fmain, Panel editor, PictureBox rule, PictureBox report) {
            m_fmain = fmain;
            m_editor = editor;
            m_editor.AutoScroll = true;
        
            m_picRule = rule;
            m_picRule.SetBounds(cUtil.mp(1), cUtil.mp(1), cUtil.mp(50), cUtil.mp(297));
            m_picRule.BackColor = Color.PeachPuff;

            m_picReport = report;
            m_picReport.SetBounds(cUtil.mp(50) + cUtil.mp(1), cUtil.mp(1), cUtil.mp(210), cUtil.mp(297));
            m_picReport.BackColor = Color.Beige;
        }
        
        private cEditor() {}

        // constants
        //
        private const String C_MODULE = "cEditor";
        private const int C_TOPBODY = 150;
        private const int C_LEFTBODY = 0;
        private const int C_MIN_HEIGHT_SECTION = 50;
        private const String C_SECTIONLINE = "Line ";

        private const int C_NOMOVE = -1111111;

        private cReport m_report;
        private CSReportPaint.cReportPaint m_paint;
        private String m_keyMoving = "";
        private CSRptEditorMoveType m_moveType;
        private String m_keySizing = "";
        private bool m_mouseButtonPress = false;
        private float m_offX = 0;
        private float m_offY = 0;
        private String m_keyObj = "";
        private String m_keyFocus = "";
        private bool m_moving = false;
        private bool m_opening = false;
        private float m_offSet = 0;

        // the first SectionLine from where we need 
        // to modify the top after moving sections. 
        // It is used only in footers.
        private int m_indexSecLnMoved = 0;

        // it is used in MoveSection to calculate
        // the positions after adding new SectionLines.
        //
        private float m_newSecLineOffSet = 0;

        private bool m_bMoveVertical = false;
        private bool m_bMoveHorizontal = false;
        private bool m_bNoMove = false;

        private String[] m_vSelectedKeys = null;
        private String[] m_vCopyKeys = null;

        private fProgress m_fProgress;
        private bool m_cancelPrinting = false;

        private int m_formIndex = 0;

        private fProperties m_fProperties;
        private fSecProperties m_fSecProperties;
        private fFormula m_fFormula;
        private fGroup m_fGroup;
        private fToolbox m_fToolBox;
        private fControls m_fControls;
        private fTreeViewCtrls m_fTreeCtrls;
        private fConnectsAux m_fConnectsAux;
        private fSearch m_fSearch;

        // names
        private int m_nextNameCtrl = 0;
        private bool m_showingProperties = false;
        private bool m_dataHasChanged = false;

        // to add new controls
        private bool m_copyControls = false;
        private bool m_copyControlsFromOtherReport = false;
        private bool m_bCopyWithoutMoving = false;

        private bool m_draging = false;
        private String m_controlName = "";
        private csRptEditCtrlType m_controlType;
        private String m_fieldName = "";
        private int m_fieldType = 0;
        private int m_fieldIndex = 0;
        private String m_formulaText = "";

        private float m_x = 0;
        private float m_y = 0;
        private bool m_keyboardMove = false;

        private int m_keyboardMoveStep = 0;

        private bool m_inMouseDown = false;

        private CSReportPaint.csETypeGrid m_typeGrid;
        
        /*
         
        // events
        // public properties

        public String getVCopyKeys(int idx) {
            return m_vCopyKeys[idx];
        }

        public int getVCopyKeysCount() {
            return m_vCopyKeys.Length;
        }

        public CSReportPaint.cReportPaint getPaint() {
            return m_paint;
        }

        public void setKeyboardMoveStep(int rhs) {
            m_keyboardMoveStep = rhs;
        }
        public bool getBMoveNoMove() {
            return m_bNoMove;
        }
        public bool getBMoveVertical() {
            return m_bMoveVertical;
        }
        public bool getBMoveHorizontal() {
            return m_bMoveHorizontal;
        }

        public int getPaperSize() {
            if (m_report == null) { return 0; }
            return m_report.getPaperInfo().getPaperSize();
        }

        public int getOrientation() {
            if (m_report == null) { return 0; }
            return m_report.getPaperInfo().getOrientation();
        }

        public int getCopies() {
            if (m_report == null) { return 0; }
            return m_report.getLaunchInfo().getCopies();
        }

        public void setPaperSize(int rhs) {
            if (m_report == null) { return; }
            m_report.getPaperInfo().setPaperSize(rhs);
        }

        public void setOrientation(int rhs) {
            if (m_report == null) { return; }
            m_report.getPaperInfo().setOrientation(rhs);
        }

        public void setCopies(int rhs) {
            if (m_report == null) { return; }
            m_report.getLaunchInfo().setCopies(rhs);
        }

        public void setCustomHeight(int rhs) {
            if (m_report == null) { return; }
            m_report.getPaperInfo().setCustomHeight(rhs);
        }

        public void setCustomWidth(int rhs) {
            if (m_report == null) { return; }
            m_report.getPaperInfo().setCustomWidth(rhs);
        }

        public int getCustomHeight() {
            if (m_report == null) { return 0; }
            return m_report.getPaperInfo().getCustomHeight();
        }

        public int getCustomWidth() {
            if (m_report == null) { return 0; }
            return m_report.getPaperInfo().getCustomWidth();
        }

        public String getFileName() {
            return m_report.getPath() + m_report.getName();
        }

        public bool getShowingProperties() {
            return m_showingProperties;
        }
        */
        public void setShowingProperties(bool rhs) {
            m_showingProperties = rhs;
        }

        public fGroup getFGroup() {
            return m_fGroup;
        }
        
        public void setFGroup(fGroup rhs) {
            m_fGroup = rhs;
        }
        
        public cReport getReport() {
            return m_report;
        }

        public bool getDataHasChanged() {
            return m_dataHasChanged;
        }

        public void setDataHasChanged(bool rhs) {
            m_dataHasChanged = rhs;
        }
        /*
        // private properties

        // public functions

        public void search() {
            m_fSearch = new fSearch(this);
            m_fSearch.ShowDialog();
        }

        public void moveVertical() {
            form_KeyUp(Keys.F11, false);
        }

        public void moveHorizontal() {
            form_KeyUp(Keys.F12, false);
        }

        public void moveNoMove() {
            form_KeyUp(Keys.F9, false);
        }

        public void moveAll() {
            form_KeyUp(Keys.F8, false);
        }

        // private functions
        public void showGrid(CSReportPaint.csETypeGrid typeGrid) {
            m_typeGrid = typeGrid;
            m_paint.initGrid(m_picReport, typeGrid);
        }

        public void showConnectsAux() {
            try {
                m_fConnectsAux = new fConnectsAux();

                / * TODO: this code must to be moved to fConnectsAux constructor
                 * 
                
                __TYPE_NOT_FOUND w___TYPE_NOT_FOUND = m_fConnectsAux.lvColumns;
                    w___TYPE_NOT_FOUND.ListItems.fToolbox.clear();
                    w___TYPE_NOT_FOUND.ColumnHeaders.fToolbox.clear();
                    w___TYPE_NOT_FOUND.ColumnHeaders.Add(, , "DataSource", 2500);
                    w___TYPE_NOT_FOUND.ColumnHeaders.Add(, , "StrConnect", 5000);
                * /
                for (int _i = 0; _i < m_report.getConnectsAux().size(); _i++) {
                    pAddConnectAuxToListView(m_report.getConnectsAux().getItem(_i));
                }                
                m_fConnectsAux.ShowDialog();

            } catch (Exception ex) {
                cError.mngError(ex, "showConnectsAux", C_MODULE, "");
                m_fConnectsAux.Close();
                m_fConnectsAux = null;
            }
        }

        private void pAddConnectAuxToListView(cReportConnect connect) {
            m_fConnectsAux.AddConnect(connect.getDataSource(), connect.getStrConnect());
        }

        private void form_KeyUp(Keys keyCode, bool ctrlKey) {
            // if we are in edit mode we do nothing
            //
            if (TxEdit.Visible) { return; }

            switch (keyCode) {

                case Keys.F2:
                    editText();

                    break;
                case Keys.Delete:
                    deleteObj();

                    break;
                case Keys.Escape:
                    endDraging();

                    break;
                case Keys.F11:
                    m_bMoveVertical = true;
                    m_bMoveHorizontal = false;
                    cGlobals.setStatus();

                    break;
                case Keys.F12:
                    m_bMoveHorizontal = true;
                    m_bMoveVertical = false;
                    cGlobals.setStatus();

                    break;
                case Keys.F8:
                    m_bMoveHorizontal = false;
                    m_bMoveVertical = false;
                    cGlobals.setStatus();

                    break;
                case Keys.F9:
                    m_bNoMove = !m_bNoMove;
                    cGlobals.setStatus();

                    break;
                case Keys.F4:
                    showProperties();

                    break;
                case Keys.C:
                    if (ctrlKey) {
                        copy();
                    }

                    break;
                case Keys.V:
                    if (ctrlKey) {
                        paste(false);
                    }

                    break;
            }

            Application.DoEvents();
        }

        // TODO: this functionality must to be moved to fConnectsAux
        //
        private void m_fConnectsAux_AddConnect() {
            try {

                cReportConnect rptConnect = null;
                rptConnect = new cReportConnect();

                if (!configConnection(rptConnect)) { return; }

                m_report.getConnectsAux().add(rptConnect);

                pAddConnectAuxToListView(rptConnect);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fConnectsAux_AddConnect", C_MODULE, "");        
            }
        }

        // TODO: this functionality must to be moved to fConnectsAux
        //
        private void m_fConnectsAux_DeleteConnect() {
            try {
                int index = 0;

                if (m_fConnectsAux.lvColumns.SelectedItem == null) {
                    cWindow.msgWarning("Select one connection", "Additional connections");
                    return;
                }

                // TODO: this functionality must to be refactored to separate the
                //       UI code from the business code
                //
                index = m_fConnectsAux.lvColumns.SelectedItem.index;

                m_report.getConnectsAux().remove(index);

                m_fConnectsAux.lvColumns.ListItems.Remove(index);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fConnectsAux_DeleteConnect", C_MODULE, "");
            }
        }

        // TODO: this functionality must to be moved to fConnectsAux
        //
        private void m_fConnectsAux_EditConnect() {
            try {
                int index = 0;

                if (m_fConnectsAux.lvColumns.SelectedItem == null) {
                    cWindow.msgWarning("Select one connection", "Additional Connections");
                    return;
                }

                index = m_fConnectsAux.lvColumns.SelectedItem.index;

                if (!configConnection(m_report.getConnectsAux(index))) { return; }

                //TODO:** can't found type for with block
                //With m_fConnectsAux.lvColumns.SelectedItem
                __TYPE_NOT_FOUND w___TYPE_NOT_FOUND = m_fConnectsAux.lvColumns.SelectedItem;
                    w___TYPE_NOT_FOUND.Text = m_report.getConnectsAux(index).DataSource;
                    w___TYPE_NOT_FOUND.SubItems(1) = m_report.getConnectsAux(index).strConnect;
                // {end with: w___TYPE_NOT_FOUND}

            } catch (Exception ex) {
                cError.mngError(ex, "m_fConnectsAux_EditConnect", C_MODULE, "");
            }
        }

        private void m_fControls_EditCtrl(String ctrlKey) {
            try {

                pSelectCtrl(ctrlKey);
                showProperties();
                m_fControls.clear();
                m_fControls.addCtrls(m_report);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fControls_EditCtrl", C_MODULE, "");
            }
        }

        private void m_fSearch_EditCtrl(String ctrlKey) {
            try {

                pSelectCtrl(ctrlKey);
                showProperties();

            } catch (Exception ex) {
                cError.mngError(ex, "m_fSearch_EditCtrl", C_MODULE, "");
            }
        }

        private void m_fSearch_EditSection(String secKey) {
            try {

                bool bIsSecLn = false;

                pSelectSection(secKey, bIsSecLn);

                if (bIsSecLn) {
                    showSecLnProperties();
                } 
                else {
                    showProperties();
                }

            } catch (Exception ex) {
                cError.mngError(ex, "m_fSearch_EditSection", C_MODULE, "");
            }
        }

        private void m_fSearch_SetFocusCtrl(String ctrlKey) {
            try {

                pSelectCtrl(ctrlKey);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fSearch_SetFocusCtrl", C_MODULE, "");
            }
        }

        private void m_fSearch_SetFocusSec(String secKey) {
            try {

                pSelectSection(secKey, false);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fSearch_SetFocusSec", C_MODULE, "");
            }
        }

        private void m_fTreeCtrls_EditCtrl(String ctrlKey) {
            try {

                pSelectCtrl(ctrlKey);
                showProperties();
                m_fTreeCtrls.clear();
                m_fTreeCtrls.addCtrls(m_report);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fTreeCtrls_EditCtrl", C_MODULE, "");
            }
        }

        private void m_fControls_SetFocusCtrl(String ctrlKey) {
            try {

                pSelectCtrl(ctrlKey);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fControls_SetFocusCtrl", C_MODULE, "");
            }
        }

        private void m_fTreeCtrls_EditSection(String secKey) {
            try {

                bool bIsSecLn = false;

                pSelectSection(secKey, bIsSecLn);

                if (bIsSecLn) {
                    showSecLnProperties();
                } 
                else {
                    showProperties();
                }
                m_fTreeCtrls.clear();
                m_fTreeCtrls.addCtrls(m_report);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fTreeCtrls_EditCtrl", C_MODULE, "");
            }
        }

        private void m_fTreeCtrls_SetFocusCtrl(String ctrlKey) {
            try {

                pSelectCtrl(ctrlKey);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fTreeCtrls_SetFocusCtrl", C_MODULE, "");
            }
        }

        private void m_fTreeCtrls_SetFocusSec(String secKey) {
            try {

                pSelectSection(secKey, false);

            } catch (Exception ex) {
                cError.mngError(ex, "m_fTreeCtrls_SetFocusSec", C_MODULE, "");
            }
        }

        private void pSelectCtrl(String ctrlKey) {
            bool bWasRemoved = false;
            String sKey = "";

            G.redim(m_vSelectedKeys, 0);
            sKey = getReport().getControls().item(ctrlKey).getKeyPaint();
            pAddToSelected(sKey, false, out bWasRemoved);

            if (bWasRemoved) { sKey = ""; }

            m_keyFocus = sKey;
            m_keyObj = sKey;
            m_paint.setFocus(m_keyFocus, m_picReport, true);
        }

        private void pSelectSection(String secKey, out bool bIsSecLn) {
            bool bWasRemoved = false;
            String sKey = "";

            bIsSecLn = false;

            G.redim(m_vSelectedKeys, 0);

            if (!m_report.getHeaders().item(secKey) == null) {
                sKey = m_report.getHeaders().item(secKey).getKeyPaint();
            } 
            else if (!m_report.getGroupsHeaders().item(secKey) == null) {
                sKey = m_report.getGroupsHeaders().item(secKey).getKeyPaint();
            } 
            else if (!m_report.getDetails().item(secKey) == null) {
                sKey = m_report.getDetails().item(secKey).getKeyPaint();
            } 
            else if (!m_report.getGroupsFooters().item(secKey) == null) {
                sKey = m_report.getGroupsFooters().item(secKey).getKeyPaint();
            } 
            else if (!m_report.getFooters().item(secKey) == null) {
                sKey = m_report.getFooters().item(secKey).getKeyPaint();
            } 
            else {
                cReportSectionLine secLn = null;
                cReportSection sec = null;

                bIsSecLn = true;

                secLn = pGetSecLnFromKey(secKey, m_report.getHeaders(), sec);
                if (secLn != null) {
                    sKey = secLn.getKeyPaint();
                    if (sKey == "") {
                        sKey = sec.getKeyPaint();
                    }
                } 
                else {
                    secLn = pGetSecLnFromKey(secKey, m_report.getGroupsHeaders(), sec);
                    if (secLn != null) {
                        sKey = secLn.getKeyPaint();
                        if (sKey == "") {
                            sKey = sec.getKeyPaint();
                        }
                    } 
                    else {
                        secLn = pGetSecLnFromKey(secKey, m_report.getDetails(), sec);
                        if (secLn != null) {
                            sKey = secLn.getKeyPaint();
                            if (sKey == "") {
                                sKey = sec.getKeyPaint();
                            }
                        } 
                        else {
                            secLn = pGetSecLnFromKey(secKey, m_report.getGroupsFooters(), sec);
                            if (secLn != null) {
                                sKey = secLn.getKeyPaint();
                                if (sKey == "") {
                                    sKey = sec.getKeyPaint();
                                }
                            } 
                            else {
                                secLn = pGetSecLnFromKey(secKey, m_report.getFooters(), sec);
                                if (secLn != null) {
                                    sKey = secLn.getKeyPaint();
                                    if (sKey == "") {
                                        sKey = sec.getKeyPaint();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (sKey == "") { return; }

            pAddToSelected(sKey, false, out bWasRemoved);
            if (bWasRemoved) { sKey = ""; }

            m_keyFocus = sKey;
            m_keyObj = sKey;
            m_paint.setFocus(m_keyFocus, m_picReport, true);
        }

        private cReportSectionLine pGetSecLnFromKey(
            String secKey, 
            cReportSections sections, 
            out cReportSection rtnSec) 
        { 
            cReportSection sec = null;
            for (int _i = 0; _i < sections.size(); _i++) {
                sec = sections.getItem(_i);
                if (!sec.getSectionLines().item(secKey) == null) {
                    rtnSec = sec;
                    return sec.getSectionLines().item(secKey);
                }
            }
        }

        private void m_fFormula_CheckSintaxis(out bool cancel, String code) { 
            cReportFormula f = null;
            f = new cReportFormula();
            if (m_fProperties != null) {
                f.setName(m_fProperties.getFormulaName());
            } 
            else {
                f.setName(m_fSecProperties.getFormulaName());
            }
            f.setText(code);
            cancel = !m_report.getCompiler().checkSintax(f);
        }

        private void m_fGroup_ShowHelpDbField() {
            int nIndex = 0;
            int nFieldType = 0;
            String sField = "";

            sField = m_fGroup.TxDbField.Text;
            nFieldType = m_fGroup.getFieldType();
            nIndex = m_fGroup.getIndex();

            if (!mPublic.showDbFields(sField, nFieldType, nIndex, Me)) { return; }

            m_fGroup.TxDbField.Text = sField;
            m_fGroup.setFieldType(nFieldType);
            m_fGroup.setIndex(nIndex);
        }

        private void m_fProperties_ShowEditFormula(String formula, out bool cancel) {
            pShowEditFormula(formula, cancel);
        }

        private void m_fProperties_ShowHelpChartField(out bool cancel, Object ctrl, int idx) {
            int nIndex = 0;
            int nFieldType = 0;
            String sField = "";

            sField = ctrl.Text;
            nFieldType = m_fProperties.getChartFieldType(idx);
            nIndex = m_fProperties.getChartIndex(idx);

            cancel = !mPublic.showDbFields(sField, nFieldType, nIndex, Me);
            if (cancel) { return; }

            ctrl.Text = sField;
            m_fProperties.setChartFieldType(idx, nFieldType);
            m_fProperties.setChartIndex(idx, nIndex);
        }

        private void m_fProperties_ShowHelpChartGroupField(out bool cancel) {
            int nIndex = 0;
            int nFieldType = 0;
            String sField = "";

            sField = m_fProperties.TxDbFieldGroupValue.Text;
            nFieldType = m_fProperties.getChartGroupFieldType();
            nIndex = m_fProperties.getChartGroupIndex();

            cancel = !mPublic.showDbFields(sField, nFieldType, nIndex, Me);
            if (cancel) { return; }

            m_fProperties.TxDbFieldGroupValue.Text = sField;
            m_fProperties.setChartGroupFieldType(nFieldType);
            m_fProperties.setChartGroupIndex(nIndex);
        }

        private void m_fSecProperties_ShowEditFormula(String formula, out bool cancel) {
            pShowEditFormula(formula, out cancel);
        }

        private void pShowEditFormula(String formula, out bool cancel) {
            try {

                cReportFormulaType f = null;
                cReportControl c = null;

                if (m_fFormula == null) { 
                    m_fFormula = new fFormula();
                    // TODO: set event handlers for fFormula
                }

                // TODO: this functionality has to be moved to fFormula
                //

                    // Load formulas in the tree
                    m_fFormula.createArbol();

                    for (int _i = 0; _i < m_report.getFormulaTypes().size(); _i++) {
                        f = m_report.FormulaTypes.getItem(_i);
                        m_fFormula.addFormula(f.getId(), f.getName(), f.getNameUser(), f.getDecrip(), f.getHelpContextId());
                    }

                    for (int _i = 0; _i < m_report.getControls().size(); _i++) {
                        c = m_report.Controls.getItem(_i);
                        if (c.getControlType() == csRptControlType.CSRPTCTFIELD) {
                            m_fFormula.addDBField(c.getName(), c.getField().getName());
                        } 
                        else if (c.getControlType() == csRptControlType.CSRPTCTLABEL) {
                            m_fFormula.addLabel(c.getName());
                        }
                    }

                    m_fFormula.ctxFormula.Text = formula;

                    m_fFormula.expandTree();

                    CenterForm(m_fFormula);

                //
                // TODO: end functionality to move 
                
                m_fFormula.Show(vbModal);

                cancel = !m_fFormula.getOk();

                if (!cancel) {
                    formula = m_fFormula.ctxFormula.Text;
                }

            } catch (Exception ex) {
                cError.mngError(ex, "m_fProperties_ShowEditFormula", C_MODULE, "");
                Unload(m_fFormula);
            }
        }

        private void m_fSecProperties_UnloadForm() {
            m_fSecProperties = null;
        }

        private void m_fToolBox_AddControl(
            String controlName, 
            csRptEditCtrlType controlType, 
            String fieldName, 
            String formulaText, 
            int fieldType, 
            int fieldIndex) 
        {
            beginDraging();
            m_controlName = controlName;
            m_controlType = controlType;
            m_fieldName = fieldName;
            m_formulaText = formulaText;
            m_fieldIndex = fieldIndex;
            m_fieldType = fieldType;
        }

        private void m_fTreeCtrls_UpdateFormulaHide(String ctrlKey, String formula) {
            m_report.getControls().item(ctrlKey).getFormulaHide().setText(formula);
        }

        private void m_fTreeCtrls_UpdateFormulaValue(String ctrlKey, String formula) {
            m_report.getControls().item(ctrlKey).getFormulaValue().setText(formula);
        }

        private void m_fTreeCtrls_UpdateSectionFormulaHide(String secKey, String formula) {

            if (!m_report.getHeaders().item(secKey) == null) {
                m_report.getHeaders().item(secKey).getFormulaHide().setText(formula);
            } 
            else if (!m_report.getGroupsHeaders().item(secKey) == null) {
                m_report.getGroupsHeaders().item(secKey).getFormulaHide().setText(formula);
            } 
            else if (!m_report.getDetails().item(secKey) == null) {
                m_report.getDetails().item(secKey).getFormulaHide().setText(formula);
            } 
            else if (!m_report.getGroupsFooters().item(secKey) == null) {
                m_report.getGroupsFooters().item(secKey).getFormulaHide().setText(formula);
            } 
            else if (!m_report.getFooters().item(secKey) == null) {
                m_report.getFooters().item(secKey).getFormulaHide().setText(formula);
            } 
            else {
                cReportSectionLine secLn = null;
                cReportSection sec = null;

                secLn = pGetSecLnFromKey(secKey, m_report.getHeaders(), sec);
                if (secLn != null) {
                    secLn.getFormulaHide().setText(formula);
                } 
                else {
                    secLn = pGetSecLnFromKey(secKey, m_report.getGroupsHeaders(), sec);
                    if (secLn != null) {
                        secLn.getFormulaHide().setText(formula);
                    } 
                    else {
                        secLn = pGetSecLnFromKey(secKey, m_report.getDetails(), sec);
                        if (secLn != null) {
                            secLn.getFormulaHide().setText(formula);
                        } 
                        else {
                            secLn = pGetSecLnFromKey(secKey, m_report.getGroupsFooters(), sec);
                            if (secLn != null) {
                                secLn.getFormulaHide().setText(formula);
                            } 
                            else {
                                secLn = pGetSecLnFromKey(secKey, m_report.getFooters(), sec);
                                if (secLn != null) {
                                    secLn.getFormulaHide().setText(formula);
                                }
                            }
                        }
                    }
                }
            }

        }

        private void m_picReport_KeyDown(int keyCode, int shift) {
            try {

                // only process arrow keys 
                switch (keyCode) {
                    case Keys.Up:
                        break;
                    case Keys.Down:
                        break;
                    case Keys.Left:
                        break;
                    case Keys.Right:
                        break;
                    default:
                        return;
                        break;
                }

                float x = 0;
                float y = 0;

                if (m_vSelectedKeys.length < 1) { return; }

                if (!m_keyboardMove) {
                    CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[1]).getAspect();
                    y = aspect.Top;
                    x = aspect.Left;
                } 
                else {
                    y = m_y;
                    x = m_x;
                }

                // resize
                //
                if (shift && vbShiftMask) {

                    if (m_keySizing == "") {
                        m_keySizing = m_paint.getPaintObject(m_vSelectedKeys[1]).getKey();
                    }

                    if (!m_keyboardMove) {

                        CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[1]).getAspect();
                        y = y + aspect.Height;
                        x = x + aspect.Width;

                        pSetMovingFromKeyboard(x, y);

                        if (m_keySizing == "") {
                            m_keySizing = m_paint.getPaintObject(m_vSelectedKeys[1]).getKey();
                        }

                        switch (keyCode) {

                            case Keys.Down:
                            case Keys.Up:
                                m_keyMoving = "";
                                m_moveType = CSRptEditorMoveType.csRptEdMovDown;
                                break;
                            case Keys.Right:
                            case Keys.Left:
                                m_keyMoving = "";
                                m_moveType = CSRptEditorMoveType.csRptEdMovRight;
                                break;
                        }
                    }

                    switch (keyCode) {
                        case Keys.Up:
                            y = y - m_keyboardMoveStep;
                            break;
                        case Keys.Down:
                            y = y + m_keyboardMoveStep;
                            break;
                        case Keys.Left:
                            x = x - m_keyboardMoveStep;
                            break;
                        case Keys.Right:
                            x = x + m_keyboardMoveStep;
                            break;
                    }

                    // move
                    //
                } 
                else {

                    if (!m_keyboardMove) {
                        pSetMovingFromKeyboard(x, y);
                    }

                    if (m_keyMoving == "") {
                        m_keyMoving = m_paint.getPaintObject(m_vSelectedKeys[1]).getKey();
                    }

                    switch (keyCode) {
                        case Keys.Up:
                            y = y - m_keyboardMoveStep;
                            break;
                        case Keys.Down:
                            y = y + m_keyboardMoveStep;
                            break;
                        case Keys.Left:
                            x = x - m_keyboardMoveStep;
                            break;
                        case Keys.Right:
                            x = x + m_keyboardMoveStep;
                            break;
                    }
                }

                m_picReport_MouseMove(MouseButtons.Left, 0, x, y);
                m_x = x;
                m_y = y;

                m_keyboardMove = true;

            } catch (Exception ex) {
                cError.mngError(ex, "m_picReport_KeyDown", C_MODULE, "");
            }
        }

        private void pSetMovingFromKeyboard(float x, float y) {

            m_keyMoving = m_keyFocus;

            CSReportPaint.cReportPaintObject po = m_paint.getPaintObject(m_keyMoving);

            switch (po.Tag) {
                case cGlobals.C_KEY_DETAIL:
                case cGlobals.C_KEY_FOOTER:
                case cGlobals.C_KEY_HEADER:
                    m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;
                    m_picReport.MousePointer = vbSizeNS;

                    break;
                case Else:
                    if (po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONDETAIL 
                        || po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONHEADER 
                        || po.RptType == CSReportPaint.csRptPaintRptTypeGROUPSECTIONHEADER 
                        || po.RptType == CSReportPaint.csRptPaintRptTypeGROUPSECTIONFOOTER 
                        || po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONFOOTER) {

                        m_picReport.MousePointer = vbSizeNS;
                        m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;

                    } 
                    else if (po.RptType == csESectionLineTypes.C_KEY_SECLN_HEADER 
                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_DETAIL 
                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_FOOTER 
                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_GROUPH 
                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_GROUPF) {

                        m_picReport.MousePointer = vbSizeNS;
                        m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;

                    } 
                    else {
                        m_moveType = CSRptEditorMoveType.csRptEdMovTAll;
                        m_picReport.MousePointer = vbSizeAll;
                    }
                    break;
            }

            CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_keyMoving).getAspect();
            m_offX = x - aspect.Left;
            m_offY = y - (aspect.Top - aspect.OffSet);

            m_keyObj = m_keyMoving;

            mPublic.setEditAlignTextState(m_vSelectedKeys.length);
            mPublic.setEditAlignCtlState(m_vSelectedKeys.length > 1);
            pSetEditAlignValue();
            pSetFontBoldValue();

        }

        private void m_picReport_KeyUp(int keyCode, bool ctrlKey) {
            if (m_keyboardMove) {
                m_keyboardMove = false;
                m_picReport_MouseUp(MouseButtons.Left, 0, m_x, m_y);
            }
        }

        private void m_picReport_MouseDown(MouseButtons button, bool ctrlKey, float x, float y) {
            try {

                String sKey = "";
                bool bClearSelected = false;
                String lastKeyMoving = "";
                String lastKeyObj = "";

                // to avoid reentrancy
                if (m_opening) { return; }

                m_inMouseDown = true;

                if (m_draging) {
                    addControlEnd(x, y);
                    endDraging();
                }

                endEditText(false);

                bClearSelected = pClearSelected(button, shift, x, y);

                if (button == MouseButtons.Left) {

                    lastKeyObj = m_keyObj;
                    m_keyObj = "";

                    sKey = IIf(m_keyMoving != "", m_keyMoving, m_keySizing);

                    // to force focus in the header
                    if (sKey == "") {
                        m_paint.pointIsInObject(x, y, sKey);

                        if (sKey != "") {

                            CSReportPaint.cReportPaintObject po = m_paint.getPaintObject(sKey);
                            lastKeyMoving = m_keyMoving;
                            m_keyMoving = sKey;

                            switch (po.Tag) {
                                case cGlobals.C_KEY_DETAIL:
                                case cGlobals.C_KEY_FOOTER:
                                case cGlobals.C_KEY_HEADER:

                                    // only if no controls are selected
                                    //
                                    if (ctrlKey) {

                                        if (m_vSelectedKeys.length) { return; }
                                        if (LenB(m_vSelectedKeys[0])) { return; }
                                        m_keyMoving = lastKeyMoving;
                                        m_keyObj = lastKeyObj;
                                        return;
                                    }

                                    m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;
                                    m_picReport.MousePointer = vbSizeNS;

                                    break;
                                case Else:
                                    if (po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONDETAIL 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONHEADER 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeGROUPSECTIONHEADER 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeGROUPSECTIONFOOTER 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONFOOTER) {

                                        // only if no controls are selected
                                        //
                                        if (ctrlKey) {

                                            if (m_vSelectedKeys.length) { return; }
                                            if (LenB(m_vSelectedKeys[0])) { return; }
                                            m_keyMoving = lastKeyMoving;
                                            m_keyObj = lastKeyObj;
                                            return;
                                        }

                                        m_picReport.MousePointer = vbSizeNS;
                                        m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;

                                    } 
                                    else if (po.RptType == csESectionLineTypes.C_KEY_SECLN_HEADER 
                                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_DETAIL 
                                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_FOOTER 
                                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_GROUPH 
                                            || po.RptType == csESectionLineTypes.C_KEY_SECLN_GROUPF) {

                                        // only if no controls are selected
                                        //
                                        if (ctrlKey) {

                                            if (m_vSelectedKeys.length) { return; }
                                            if (LenB(m_vSelectedKeys[0])) { return; }
                                            m_keyMoving = lastKeyMoving;
                                            m_keyObj = lastKeyObj;
                                            return;
                                        }

                                        m_picReport.MousePointer = vbSizeNS;
                                        m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;

                                    } 
                                    else {
                                        m_moveType = CSRptEditorMoveType.csRptEdMovTAll;
                                        m_picReport.MousePointer = vbSizeAll;
                                    }
                                    break;
                            }                            
                        }
                    }

                    bool bWasRemoved = false;
                    pAddToSelected(m_keyMoving, ctrlKey, bWasRemoved);

                    if (bWasRemoved) { sKey = ""; }

                    if (sKey != "") {
                        CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(sKey).getAspect();
                        m_offX = x - aspect.Left;
                        m_offY = y - (aspect.Top - aspect.OffSet);
                    }

                    m_keyFocus = sKey;
                    m_keyObj = sKey;
                    m_paint.setFocus(m_keyFocus, m_picReport, bClearSelected);

                } 
                else if (button == MouseButtons.Right) {

                    m_keySizing = "";
                    m_keyMoving = "";
                    m_keyObj = "";

                    if (m_paint.pointIsInObject(x, y, sKey)) {
                        m_keyObj = sKey;

                        bClearSelected = pSetSelectForRightBttn();

                        m_keyFocus = sKey;
                        m_paint.setFocus(m_keyFocus, m_picReport, bClearSelected);

                        CSReportPaint.cReportPaintObject po = m_paint.getPaintObject(sKey);

                        if (m_paint.paintObjIsSection(sKey)) {

                            bool noDelete = false;

                            switch (po.Tag) {
                                // this sections can not be moved
                                case cGlobals.C_KEY_HEADER:
                                case cGlobals.C_KEY_DETAIL:
                                case cGlobals.C_KEY_FOOTER:
                                    noDelete = true;
                                    break;
                                
                                case Else:
                                    noDelete = false;
                                    break;
                            }

                            bool isGroup = false;
                            bool isSecLn = false;

                            pGetSection(isGroup, isSecLn);

                            if (isSecLn) { noDelete = true; }

                            showPopMenuSection(noDelete, isGroup);
                        } 
                        else {
                            showPopMenuControl(true);
                        }
                    }
                    else {
                        showPopMenuControl(false);
                    }
                }

                mPublic.setEditAlignTextState(m_vSelectedKeys.length);
                mPublic.setEditAlignCtlState(m_vSelectedKeys.length > 1);
                pSetEditAlignValue();
                pSetFontBoldValue();

            } catch (Exception ex) {
                cError.mngError(ex, "m_picReport_MouseDown", C_MODULE, "");
                m_inMouseDown = false;
            }
        }

        public void setFontBold() {
            int bBold = 0;
            bool bBoldValue = false;
            int i = 0;

            bBold = -2;

            for (i = 1; i <= m_vSelectedKeys.length; i++) {
                CSReportPaint.cReportFont font = m_paint.getPaintObject(m_vSelectedKeys[i]).getAspect().getFont();

                if (bBold == -2) {
                    bBold = font.Bold;
                } 
                else if (bBold != font.Bold) {
                    bBold = -2;
                    break;
                }                
            }

            if (bBold == -2) {
                bBoldValue = true;
            } 
            else {
                bBoldValue = bBold == 0;
            }

            CSReportPaint.cReportPaintObject paintObject = null;
            cReportControl rptCtrl = null;

            for (i = 1; i <= m_vSelectedKeys.length; i++) {

                paintObject = m_paint.getPaintObject(m_vSelectedKeys[i]);
                rptCtrl = m_report.getControls().item(paintObject.getTag());
                rptCtrl.getLabel().getAspect().getFont().setBold(bBoldValue);
                paintObject.getAspect().getFont().setBold(bBoldValue);
            }

            m_dataHasChanged = true;
            refreshAll();
            pSetFontBoldValue();
        }

        public void pSetFontBoldValue() {
            int bBold = 0;
            int i = 0;

            bBold = -2;

            for (i = 1; i <= m_vSelectedKeys.length; i++) {
                CSReportPaint.cReportFont font = m_paint.getPaintObject(m_vSelectedKeys[i]).getAspect().getFont();

                if (bBold == -2) {
                    bBold = font.Bold;
                } 
                else if (bBold != font.Bold) {
                    bBold = -2;
                    break;
                }
            }

            mPublic.setEditFontBoldValue(bBold);
        }

        public void controlsAlign(csECtlAlignConst align) {
            int i = 0;
            CSReportPaint.cReportPaintObject paintObject = null;
            cReportControl rptCtrl = null;

            int top = 0;
            int left = 0;

            int newTop = 0;
            int newLeft = 0;
            int height = 0;
            int width = 0;

            switch (align) {

                case csECtlAlignHeight:
                case csECtlAlignWidth:

                    CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[1]).getAspect();
                    height = aspect.height;
                    width = aspect.width;
                    break;

                case csECtlAlignVertical:
                case csECtlAlignHorizontal:

                    CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[1]).getAspect();
                    newTop = aspect.top;
                    newLeft = aspect.left;
                    break;

                default:

                    switch (align) {
                        case csECtlAlignLeft:
                            newLeft = 100000;
                            break;
                        case csECtlAlignRight:
                            newLeft = 0;
                            break;
                        case csECtlAlignTop:
                            newTop = 100000;
                            break;
                        case csECtlAlignBottom:
                            newTop = 0;
                            break;
                    }

                    for (i = 1; i <= m_vSelectedKeys.length; i++) {

                        CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[i]).getAspect();
                        top = aspect.top;
                        left = aspect.left;

                        switch (align) {
                            case csECtlAlignLeft:
                                if (left < newLeft) { newLeft = left; }
                                break;
                            case csECtlAlignRight:
                                if (left > newLeft) { newLeft = left; }
                                break;
                            case csECtlAlignTop:
                                if (top < newTop) { newTop = top; }
                                break;
                            case csECtlAlignBottom:
                                if (top > newTop) { newTop = top; }
                                break;
                        }
                    }

                    break;
            }

            for (i = 1; i <= m_vSelectedKeys.length; i++) {

                paintObject = m_paint.getPaintObject(m_vSelectedKeys[i]);
                rptCtrl = m_report.getControls().item(paintObject.getTag());

                switch (align) {

                    case csECtlAlignHeight:
                        rptCtrl.getLabel().getAspect().height = height;
                        paintObject.getAspect().height = height;
                        break;

                    case csECtlAlignWidth:
                        rptCtrl.getLabel().getAspect().width = width;
                        paintObject.getAspect().width = width;
                        break;

                    case csECtlAlignLeft:
                    case csECtlAlignRight:
                    case csECtlAlignHorizontal:
                        rptCtrl.getLabel().getAspect().left = newLeft;
                        paintObject.getAspect().left = newLeft;
                        break;

                    case csECtlAlignTop:
                    case csECtlAlignBottom:
                    case csECtlAlignVertical:
                        rptCtrl.getLabel().getAspect().top = newTop;
                        paintObject.getAspect().top = newTop;
                        break;
                }
            }

            m_dataHasChanged = true;
            refreshAll();
        }

        public void textAlign(HorizontalAlignment align) {
            int i = 0;
            CSReportPaint.cReportPaintObject paintObject = null;
            cReportControl rptCtrl = null;

            for (i = 1; i <= m_vSelectedKeys.length; i++) {

                paintObject = m_paint.getPaintObject(m_vSelectedKeys[i]);
                rptCtrl = m_report.getControls().item(paintObject.getTag());

                rptCtrl.getLabel().getAspect().align = align;
                paintObject.getAspect().align = align;
            }

            m_dataHasChanged = true;
            refreshAll();
            pSetEditAlignValue();
        }

        private void pSetEditAlignValue() {
            int align = -1;
            int i = 0;

            for (i = 1; i <= m_vSelectedKeys.length; i++) {
                CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[i]).getAspect();

                if (align == -1) {
                    align = aspect.align;
                } 
                else if (align != aspect.align) {
                    align = -2;
                    break;
                }
            }
            mPublic.setEditAlignValue(align);
        }

        private void pAddToSelected(String sKey, bool ctrlKey, out bool bWasRemoved) {

            int i = 0;
            if (sKey == "") { return; }

            bWasRemoved = false;

            if (ctrlKey) {

                for (i = 1; i <= m_vSelectedKeys.length; i++) {

                    if (m_vSelectedKeys[i] == sKey) {
                        pRemoveFromSelected(sKey);
                        bWasRemoved = true;
                        return;
                    }
                }
            } 
            else {
                if (pAllreadySelected(sKey)) { return; }
            }

            G.redimPreserve(m_vSelectedKeys, m_vSelectedKeys.length + 1);
            m_vSelectedKeys[UBound(m_vSelectedKeys)] = sKey;
        }

        private bool pAllreadySelected(String sKey) {
            int i = 0;

            if (sKey == "") {
                return true;
            }

            for (i = 1; i <= m_vSelectedKeys.length; i++) {
                if (m_vSelectedKeys[i] == sKey) {
                    return true;
                }
            }
            return false;
        }

        private void pRemoveFromSelected(String sKey) {
            int i = 0;

            for (i = 1; i <= m_vSelectedKeys.length; i++) {
                if (m_vSelectedKeys[i] == sKey) {
                    break;
                }
            }

            if (i > m_vSelectedKeys.length) { return; }
            for (i = i + 1; i <= m_vSelectedKeys.length; i++) {
                m_vSelectedKeys[i - 1] == m_vSelectedKeys[i];
            }
            if (m_vSelectedKeys.length > 0) {
                G.redimPreserve(m_vSelectedKeys, m_vSelectedKeys.length - 1);
            } 
            else {
                G.redim(m_vSelectedKeys, 0);
            }

            m_paint.removeFromSelected(sKey, m_picReport);
        }

        private bool pClearSelected(MouseButtons button, bool ctrlKey, float x, float y) {
            bool _rtn = false;
            String sKey = "";
            int i = 0;

            if (!ctrlKey && button != MouseButtons.Right) {
                m_paint.pointIsInObject(x, y, sKey);
                for (i = 1; i <= m_vSelectedKeys.length; i++) {
                    if (m_vSelectedKeys[i] == sKey) {
                        return false;
                    }
                }
                G.redim(m_vSelectedKeys, 0);
                return true;
            }
            return false;
        }

        private void pShowMoveAll(float x, float y) {
            int i = 0;
            int offsetTop = 0;
            int offsetLeft = 0;
            int firstLeft = 0;
            int firstTop = 0;
            bool clear = false;
            int offSet2 = 0;

            if (m_vSelectedKeys.length == 0) { return; }

            CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_keyMoving).getAspect();
            firstLeft = aspect.Left;
            firstTop = aspect.Top;

            clear = true;

            for (i = m_vSelectedKeys.length; i <= 1; i--) {

                CSReportPaint.cReportAspect aspect = m_paint.getPaintObject(m_vSelectedKeys[i]).getAspect();
                offsetLeft = pGetOffsetLeftFromControls(firstLeft, aspect.Left);
                offsetTop = pGetOffsetTopFromControls(firstTop, aspect.Top);
                offSet2 = aspect.OffSet;

                if (m_bMoveHorizontal) {
                    m_paint.moveObjToXYEx(m_keyMoving, 
                                            x - m_offX + offsetLeft, 
                                            firstTop - offSet2 + offsetTop, 
                                            m_picReport, 
                                            clear);
                } 
                else if (m_bMoveVertical) {
                    m_paint.moveObjToXYEx(m_keyMoving, 
                                            firstLeft + offsetLeft, 
                                            y - m_offY + offsetTop, 
                                            m_picReport, 
                                            clear);
                } 
                else {
                    m_paint.moveObjToXYEx(m_keyMoving, 
                                            x - m_offX + offsetLeft, 
                                            y - m_offY + offsetTop, 
                                            m_picReport, 
                                            clear);
                }

                if (clear) { clear = false; }
            }
        }

        private void m_picReport_MouseMove(
            MouseButtons button, 
            int shift, 
            float x, 
            float y) 
        {
            String sKey = "";
            CSReportPaint.cRptPaintRegionType rgnTp = null;

            if (m_draging) { return; }

            if (m_inMouseDown) { return; }

            if (button == MouseButtons.Left) {

                m_paint.beginMove();

                if (m_keyMoving != "") {

                    switch (m_moveType) {
                        case CSRptEditorMoveType.csRptEdMovTAll:
                            pShowMoveAll(x, y);
                            break;
                        case CSRptEditorMoveType.csRptEdMovTHorizontal:
                            m_paint.moveHorizontal(m_keyMoving, x, m_picReport);
                            break;
                        case CSRptEditorMoveType.csRptEdMovTVertical:
                            m_paint.moveVertical(m_keyMoving, y, m_picReport);
                            break;
                    }

                    m_moving = true;

                } 
                else if (m_keySizing != "") {
                    switch (m_moveType) {
                        case CSRptEditorMoveType.csRptEdMovDown:
                            m_paint.resize(m_picReport, m_keySizing, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE, y);
                            break;
                        case CSRptEditorMoveType.csRptEdMovLeft:
                            m_paint.resize(m_picReport, m_keySizing, x, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE);
                            break;
                        case CSRptEditorMoveType.csRptEdMovRight:
                            m_paint.resize(m_picReport, m_keySizing, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE, x, cGlobals.C_NO_CHANGE);
                            break;
                        case CSRptEditorMoveType.csRptEdMovUp:
                            m_paint.resize(m_picReport, m_keySizing, cGlobals.C_NO_CHANGE, y, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE);
                            break;
                        case CSRptEditorMoveType.csRptEdMovLeftDown:
                            m_paint.resize(m_picReport, m_keySizing, x, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE, y);
                            break;
                        case CSRptEditorMoveType.csRptEdMovLeftUp:
                            m_paint.resize(m_picReport, m_keySizing, x, y, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE);
                            break;
                        case CSRptEditorMoveType.csRptEdMovRightDown:
                            m_paint.resize(m_picReport, m_keySizing, cGlobals.C_NO_CHANGE, cGlobals.C_NO_CHANGE, x, y);
                            break;
                        case CSRptEditorMoveType.csRptEdMovRightUp:
                            m_paint.resize(m_picReport, m_keySizing, cGlobals.C_NO_CHANGE, y, x, cGlobals.C_NO_CHANGE);
                            break;
                    }
                    m_moving = true;
                } 
                else {
                    m_moving = false;
                }
            } 
            else {
                if (m_keyFocus != "") {
                    sKey = m_keyFocus;
                    if (m_paint.pointIsInThisObject(x, y, m_keyFocus, rgnTp)) {
                        CSReportPaint.cReportPaintObject po = m_paint.getPaintObject(sKey);

                        cReportControl ctrl = m_report.getControls().item(po.Tag);
                        pSetSbPnlCtrl(ctrl.Name, 
                                        ctrl.ControlType, 
                                        ctrl.getFormulaHide().Text, 
                                        ctrl.getFormulaValue().Text, 
                                        ctrl.HasFormulaHide, 
                                        ctrl.HasFormulaValue, 
                                        ctrl.fColumns.getField().Name);

                        if (po.PaintType == CSReportPaint.csRptPaintObjType.CSRPTPAINTOBJLINE) {
                            m_keyMoving = sKey;
                            m_keySizing = "";
                            m_picReport.MousePointer = vbSizeNS;
                        } 
                        else {
                            switch (po.Tag) {
                                case cGlobals.C_KEY_DETAIL:
                                case cGlobals.C_KEY_FOOTER:
                                case cGlobals.C_KEY_HEADER:                                        
                                    m_keyMoving = sKey;
                                    m_keySizing = "";
                                    m_picReport.MousePointer = vbSizeNS;
                                    m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;
                                    break;

                                default:

                                    if (po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONDETAIL 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONHEADER 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeGROUPSECTIONHEADER 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeGROUPSECTIONFOOTER 
                                        || po.RptType == CSReportPaint.csRptPaintRptTypeSECTIONFOOTER) {

                                        m_keyMoving = sKey;
                                        m_keySizing = "";
                                        m_picReport.MousePointer = vbSizeNS;
                                        m_moveType = CSRptEditorMoveType.csRptEdMovTVertical;
                                    } 
                                    else {

                                        switch (rgnTp) {
                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeBody:
                                                m_picReport.MousePointer = vbSizeAll;
                                                m_keyMoving = sKey;
                                                m_keySizing = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovTAll;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeDown:
                                                m_picReport.MousePointer = vbSizeNS;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovDown;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeUp:
                                                m_picReport.MousePointer = vbSizeNS;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovUp;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeLeft:
                                                m_picReport.MousePointer = vbSizeWE;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovLeft;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeRight:
                                                m_picReport.MousePointer = vbSizeWE;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovRight;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeLeftDown:
                                                m_picReport.MousePointer = vbSizeNESW;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovLeftDown;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeRightUp:
                                                m_picReport.MousePointer = vbSizeNESW;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovRightUp;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeRightDown:
                                                m_picReport.MousePointer = vbSizeNWSE;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovRightDown;
                                                break;

                                            case CSReportPaint.cRptPaintRegionType.cRptPntRgnTypeLeftUp:
                                                m_picReport.MousePointer = vbSizeNWSE;
                                                m_keySizing = sKey;
                                                m_keyMoving = "";
                                                m_moveType = CSRptEditorMoveType.csRptEdMovLeftUp;
                                                break;

                                            default:
                                                m_keySizing = "";
                                                m_keyMoving = "";
                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                    } 
                    else {
                        pSetSbPnlCtrl("");
                        m_picReport.MousePointer = vbDefault;
                        m_keySizing = "";
                        m_keyMoving = "";
                    }
                }

                if (m_paint.pointIsInObject(x, y, sKey, rgnTp)) {
                    CSReportPaint.cReportPaintObject po = m_paint.getPaintObject(sKey);
                    if (po.RptType == csRptPaintRptType.CSRPTPAINTRPTTYPECONTROL) {
                        cReportControl rptCtrl = null;
                        rptCtrl = m_report.getControls().item(po.Tag);
                        if (rptCtrl != null) {
                            pSetSbPnlCtrl(rptCtrl.getName(), 
                                            rptCtrl.getControlType(), 
                                            rptCtrl.getFormulaHide().getText(), 
                                            rptCtrl.getFormulaValue().getText(), 
                                            rptCtrl.getHasFormulaHide(), 
                                            rptCtrl.getHasFormulaValue(), 
                                            rptCtrl.getField().getName());
                        }
                    } 
                    else {
                        pSetSbPnlCtrl("");
                    }
                } 
                else {
                    pSetSbPnlCtrl("");
                }
            }
        }

        private void pSetSbPnlCtrl(
            String ctrlName, 
            csRptControlType ctrlType, 
            String formulaHide, 
            String formulaValue, 
            bool hasFormulaHide, 
            bool hasFormulaValue, 
            String fieldName) 
        {

            String msg = "";
            String strCtlType = "";

            switch (ctrlType) {
                case csRptControlType.csRptCtDbImage:
                    strCtlType = "DbImage";
                    break;
                case csRptControlType.csRptCtField:
                    strCtlType = "Field";
                    break;
                case csRptControlType.csRptCtImage:
                    strCtlType = "Image";
                    break;
                case csRptControlType.csRptCtLabel:
                    strCtlType = "Label";
                    break;
            }

            if (ctrlName != "") {
                msg = "Ctl:[" + ctrlName 
                    + "]Tipo:[" + strCtlType 
                    + "]F.Hide:[" + formulaHide.substring(1, 100) 
                    + "]Activa[" + ((bool) hasFormulaHide).ToString() 
                    + "]F.Value:[" + formulaValue.substring(1, 100) 
                    + "]Activa[" + ((bool) hasFormulaValue).ToString() 
                    + "]Field:[" + fieldName + "]";
            }
            m_fmain.setsbPnlCtrl(msg);
        }

        private void m_picReport_MouseUp(MouseButtons button, int shift, float x, float y) {
            // to avoid reentrancy
            if (m_opening) { return; }

            //----------------------------------------------------
            // MOVING
            //----------------------------------------------------
            String sKeySection = "";
            CSReportPaint.csRptPaintRptType rptType = null;

            if (m_moving) {
                if (m_keyMoving != "") {
                    switch (m_moveType) {
                        case CSRptEditorMoveType.csRptEdMovTAll:
                            if (m_bMoveVertical) {
                                pMoveAll(C_NOMOVE, y);
                            } 
                            else if (m_bMoveHorizontal) {
                                pMoveAll(x, C_NOMOVE);
                            } 
                            else {
                                pMoveAll(x, y);
                            }
                            break;

                        case CSRptEditorMoveType.csRptEdMovTHorizontal:
                            pMoveHorizontal(x);
                            break;

                        case CSRptEditorMoveType.csRptEdMovTVertical:
                            pMoveVertical(x, y);
                            break;
                    }

                //----------------------------------------------------
                // SIZING
                //----------------------------------------------------
                } 
                else if (m_keySizing != "") {
                    pSizingControl(x, y);
                }

                refreshBody();
                m_moving = false;
                refreshRule();
            }

            m_keySizing = "";
            m_keyMoving = "";
        }

        private void m_picReport_Paint() {
            m_paint.paintPicture(m_picReport);
        }

        private void m_picRule_Paint() {
            int i = 0;

            CSReportPaint.cReportPaintObjects ps = m_paint.getPaintSections();
            for (i = 1; i <= ps.count(); i++) {
                m_paint.drawRule(ps.GetNextKeyForZOrder(i), m_picRule);
            }
        }

        public void setParameters() {
            CSConnect.cConnect connect = new CSConnect.cConnect();
            cParameter param = null;

            for (int _i = 0; _i < m_report.connect.Parameters.size(); _i++) {
                param = m_report.Connect.Parameters.getItem(_i);
                CSConnect.cParameter connectParam = connect.Parameters.Add();
                connectParam.Name = param.getName();
                connectParam.Value = param.getValue();
            }

            if (m_report.connect.DataSource == "") {

                cWindow.msgWarning("Before editting the parameter info you must define a connection");
                return;

            }

            connect.strConnect = m_report.connect.strConnect;
            connect.DataSource = m_report.connect.DataSource;
            connect.DataSourceType = m_report.connect.DataSourceType;

            if (!connect.getDataSourceColumnsInfo(m_report.connect.getDataSource(), 
                                                    m_report.connect.getDataSourceType())) 
            { return; }

            mPublic.setParametersAux(connect, m_report.connect);
        }

        public void setSimpleConnection() {
            fSimpleConnect f = new fSimpleConnect();
            try {

                String strConnect = "";
                strConnect = m_report.getConnect().strConnect;
                f.setServer(cUtil.getToken("Data Source", strConnect));
                f.setDataBase(cUtil.getToken("Initial Catalog", strConnect));
                f.setUser(cUtil.getToken("User ID", strConnect));
                f.setPassword(cUtil.getToken("Password", strConnect));
                if (f.getUser() == "") {
                    f.setConnectTypeToNT();
                } 
                else {
                    f.setConnectTypeToSQL();
                }
                f.ShowDialog();

                if (!f.getOk()) { 
                    f.Close();
                }
                else {
                    m_report.getConnect().setStrConnect(f.getStrConnect);
                }

            } catch (Exception ex) {
                cError.mngError(ex, "configConnection", C_MODULE, "");
                f.Close();
            }
        }

        public bool configConnection(cReportConnect rptConnect) {
            try {

                CSConnect.cConnect connect = new CSConnect.cConnect();

                if (!connect.ShowOpenConnection()) { return _rtn; }

                refreshAll();

                if (!connect.getDataSourceColumnsInfo(connect.getDataSource(), 
                                                        connect.getDataSourceType())) {
                    return false;
                }

                if (rptConnect == null) {
                    mPublic.setParametersAux(connect, m_report.getConnect());
                } 
                else {
                    mPublic.setParametersAux(connect, rptConnect);
                }

                if (mPublic.getToolBox(this) != null) { showToolBox(); }

                return true;

            } catch (Exception ex) {
                cError.mngError(ex, "configConnection", C_MODULE, "");
                return false;
            }
        }

        public void setAllConnectToMainConnect() {
            try {

                cReportConnect connect = null;
                for (int _i = 0; _i < m_report.getConnectsAux().size(); _i++) {
                    Connect = m_report.getConnectsAux().getItem(_i);
                    connect.setStrConnect(m_report.getConnect().getStrConnect());
                }

            } catch (Exception ex) {
                cError.mngError(ex, "setAllConnectToMainConnect", C_MODULE, "");
            }
        }

        public void deleteObj(bool bDelSectionLine) {
            int i = 0;
            cReportSection sec = null;
            cReportSections secs = null;
            cReportSectionLine secLn = null;
            cReportControl ctrl = null;
            CSReportPaint.cReportPaintObject paintObj = null;

            bool isGroupFooter = false;
            bool isGroupHeader = false;
            bool isSecLn = false;

            if (m_keyFocus == "") { return; }

            cReportGroup group = null;
            cReportSection secG = null;
            if (m_paint.paintObjIsSection(m_keyFocus)) {
                if (m_paint.getPaintSections().item(m_keyFocus) == null) { return; }

                CSReportPaint.cReportPaintObject po = m_paint.getPaintSections().item(m_keyFocus);

                // first we check it is not a section line
                //
                sec = pGetSection(false, isSecLn, secLn, null, false, isGroupHeader, isGroupFooter);
                if (!isSecLn) {

                    // check it is not the last section line in this section
                    //
                    if (bDelSectionLine) {

                        sec = pGetSection(false, isSecLn, secLn, null, true, isGroupHeader, isGroupFooter);
                    }
                    if (!pCanDeleteSection(secs, sec, po.Tag)) { return; }
                }

                String what = "";

                if (isSecLn) {
                    what = "the section line";
                } 
                else {
                    what = "the section";
                }

                if (!cWindow.ask("Are yuo sure you want to delete " 
                            + what + " and all the controls it contains? ", VbMsgBoxResult.vbNo)) {
                    return;
                }

                if (isSecLn) {

                    for (int _i = 0; _i < secLn.getControls().size(); _i++) {
                        ctrl = SecLn.Controls.getItem(_i);
                        for (i = 1; i <= m_paint.getPaintObjects().count(); i++) {
                            paintObj = m_paint.getPaintObjects().item(i);
                            if (paintObj.getTag() == ctrl.getKey()) {
                                m_paint.getPaintObjects().remove(paintObj.getKey());
                                break;
                            }
                        }
                    }

                    secLn.getControls().clear();

                    // at least one section line has to be in the section
                    //
                    if (sec.getSectionLines().count() > 1) {
                        sec.getSectionLines().remove(secLn.getKey());
                    }

                } 
                else {

                    for (int _i = 0; _i < sec.getSectionLines().size(); _i++) {
                        secLn = Sec.SectionLines.getItem(_i);
                        for (int _j = 0; _j < secLn.getControls().size(); _j++) {
                            ctrl = SecLn.Controls.getItem(_j);
                            for (i = 1; i <= m_paint.getPaintObjects().count(); i++) {
                                paintObj = m_paint.getPaintObjects().item(i);
                                if (paintObj.getTag() == ctrl.getKey()) {
                                    m_paint.getPaintObjects().remove(paintObj.getKey());
                                    break;
                                }
                            }
                        }
                    }

                    // if this is a group section we need to delete the header and the footer
                    // 

                    if (isGroupFooter || isGroupHeader) {
                        if (isGroupHeader) {
                            for (int _i = 0; _i < m_report.getGroups().size(); _i++) {
                                group = m_report.Groups.getItem(_i);
                                if (group.getHeader().getKey() == sec.getKey()) { break; }
                            }
                            secG = group.getFooter();
                        } 
                        else if (isGroupFooter) {
                            for (int _i = 0; _i < m_report.getGroups().size(); _i++) {
                                group = m_report.Groups.getItem(_i);
                                if (group.getFooter().getKey() == sec.getKey()) { break; }
                            }
                            secG = group.getHeader();
                        }

                        for (int _i = 0; _i < secG.getSectionLines().size(); _i++) {
                            secLn = SecG.SectionLines.getItem(_i);
                            for (int _j = 0; _j < secLn.getControls().size(); _j++) {
                                ctrl = SecLn.Controls.getItem(_j);
                                for (i = 1; i <= m_paint.getPaintObjects().count(); i++) {
                                    paintObj = m_paint.getPaintObjects().item(i);
                                    if (paintObj.getTag() == ctrl.getKey()) {
                                        m_paint.getPaintObjects().remove(paintObj.getKey());
                                        break;
                                    }
                                }
                            }
                        }

                        for (i = 1; i <= m_paint.getPaintSections().count(); i++) {
                            paintObj = m_paint.getPaintSections().item(i);
                            if (paintObj.getTag() == secG.getKey()) {
                                m_paint.getPaintSections().remove(paintObj.getKey());
                                break;
                            }
                        }

                        m_report.getGroups().remove(group.getIndex());

                    } 
                    else {
                        secs.remove(sec.getKey());
                    }

                }

                bool bDeletePaintObj = false;

                bDeletePaintObj = true;
                if (isSecLn) {
                    bDeletePaintObj = sec.getKeyPaint() != m_keyFocus;
                }

                if (bDeletePaintObj) {

                    m_paint.getPaintSections().remove(m_keyFocus);

                    // if I have deleted the last section line in this 
                    // section I need to delete the paint object
                    // asociated with the current last section line
                    // and then to asociate this section line with
                    // the paint object of the section
                } 
                else {
                    cReportSectionLines secs = sec.getSectionLines();
                    m_paint.getPaintSections().remove(secs.Item(secs.Count).KeyPaint);
                    secs.Item(secs.Count).KeyPaint = sec.getKeyPaint();
                }

                pResetKeysFocus();
                G.redim(m_vSelectedKeys, 0);

            } 
            else {
                paintObj = m_paint.getPaintObjects().item(m_keyFocus);
                if (paintObj == null) { return; }

                if (!cWindow.ask("Confirm you want to delete the control? ", VbMsgBoxResult.vbNo)) { return; }

                for (i = 1; i <= m_vSelectedKeys.length; i++) {
                    paintObj = m_paint.getPaintObjects().item(m_vSelectedKeys[i]);
                    ctrl = m_report.getControls().item(paintObj.getTag());

                    m_paint.getPaintObjects().remove(paintObj.getKey());
                    if (ctrl == null) { return; }
                    ctrl.getSectionLine().getControls().remove(ctrl.getKey());
                }

                pResetKeysFocus();
                G.redim(m_vSelectedKeys, 0);
            }

            refreshAll();
        }

        private bool pCanDeleteSection(
            out cReportSections secs, 
            cReportSection sec, 
            String tag) 
        { 
            cReportSection secAux = null;
            
            // header
            //
            secAux = m_report.getHeaders().item(tag);
            secs = null;

            if (secAux != null) {
                if (secAux.Equals(sec) || sec == null) {
                    if (secAux.getTypeSection() == csRptTypeSection.CSRPTTPMAINSECTIONHEADER) {
                        cWindow.msgInfo("The main header can't be deleted");
                        return false;
                    }
                    secs = m_report.getHeaders();
                }
            } 
            // if we don't find the section yet
            //
            if (secs == null) {

                // footers
                //
                secAux = m_report.getFooters().item(tag);
                if (secAux != null) {
                    if (secAux.Equals(sec) || sec == null) {
                        if (secAux.getTypeSection() == csRptTypeSection.CSRPTTPMAINSECTIONFOOTER) {
                            cWindow.msgInfo("The main footer can't be deleted");
                            return false;
                        }
                        secs = m_report.getFooters();
                    }
                } 
                // if we don't find the section yet
                //
                if (secs == null) {

                    // check for groups
                    //
                    secAux = m_report.getGroupsHeaders().item(tag);
                    if (secAux != null) {
                        if (!((secAux.Equals(sec) || sec == null))) {

                            secAux = m_report.getGroupsFooters().item(tag);
                            if (secAux != null) {
                                if (!((secAux.Equals(sec) || sec == null))) {

                                    // finally the detail section can't be deleted
                                    //
                                    cWindow.msgInfo("The detail section can't be deleted");
                                    return false;
                                }
                            }
                        }
                    }
                }
            }

            return true;
        }

        private void pResetKeysFocus() {
            m_keyFocus = "";
            m_keyMoving = "";
            m_keySizing = "";
            m_picReport.MousePointer = vbDefault;
        }

        public void addDBField() {
            String sField = "";
            int nIndex = 0;
            int nFieldType = 0;

            if (!mPublic.showDbFields(sField, nFieldType, nIndex, Me)) { return; }

            beginDraging();
            m_controlName = "";
            m_controlType = csRptEditCtrlType.csRptEditField;
            m_fieldName = sField;
            m_formulaText = "";
            m_fieldIndex = nIndex;
            m_fieldType = nFieldType;
        }

        public void addLabel() {
            pAddLabelAux(csRptEditCtrlType.csRptEditLabel);
        }

        public void addImage() {
            pAddLabelAux(csRptEditCtrlType.csRptEditImage);
        }

        public void addChart() {
            pAddLabelAux(csRptEditCtrlType.csRptEditChart);
        }

        public void pAddLabelAux(csRptEditCtrlType ctlType) {
            beginDraging();
            m_controlName = "";
            m_controlType = ctlType;
            m_fieldName = "";
            m_formulaText = "";
            m_fieldIndex = 0;
            m_fieldType = 0;
        }

        private bool addControlEnd(int left, int top) {
            cReportControl ctrl = null;

            m_draging = false;

            if (m_controlType == csRptEditCtrlType.csRptEditNone) {
                return true;
            }

            m_dataHasChanged = true;

            int i = 0;
            int originalLeft = 0;
            int originalTop = 0;
            cReportControl copyCtrl = null;
            cReportControl movedCtrl = null;
            int firstCtrlLeft = 0;
            int offSet = 0;

            if (m_copyControls) {

                if (m_vCopyKeys.length == 0) { return false; }

                originalLeft = left;
                originalTop = top;

                String keyPaint = m_vCopyKeys[UBound(m_vCopyKeys)];
                String keyCtrl = m_paint.getPaintObjects(keyPaint).Tag;
                movedCtrl = m_report.getControls(keyCtrl);
                firstCtrlLeft = movedCtrl.getLabel().getAspect().left;

                for (i = m_vCopyKeys.length; i <= 1; i--) {

                    keyPaint = m_vCopyKeys[i];
                    keyCtrl = m_paint.getPaintObjects(keyPaint).Tag;
                    copyCtrl = m_report.getControls(keyCtrl);

                    // starting with the first control we move the left
                    // of every control if reach the right margin 
                    // move down a line and restart
                    //
                    offSet = pGetOffsetLeftFromControls(firstCtrlLeft, copyCtrl.getLabel().getAspect().left);
                    left = originalLeft + offSet;

                    if (m_bCopyWithoutMoving) {

                        top = copyCtrl.getLabel().getAspect().top;
                        left = copyCtrl.getLabel().getAspect().left;

                    }

                    if (left - 400 > m_picReport.Width) {
                        left = originalLeft + (offSet % originalLeft);
                        top += 100;
                    }

                    if (top > m_picReport.Height) {
                        top = m_picReport.Height - 100;
                    }

                    pAddControlEndAux(left, top, copyCtrl);

                }
                m_copyControls = false;

            } 
            else if (m_copyControlsFromOtherReport) {

                if (m_fmain.getReportCopySource() == null) { return false; }

                originalLeft = left;
                originalTop = top;

                cEditor editor = m_fmain.getReportCopySource();
                String keyPaint = editor.getVCopyKeys(editor.getVCopyKeysCount());
                String keyCtrl = editor.getPaint().getPaintObjects(keyPaint).Tag;
                movedCtrl = editor.getReport().getControls(keyCtrl);
                firstCtrlLeft = movedCtrl.getLabel().getAspect().left;

                for (i = editor.getVCopyKeysCount(); i <= 1; i--) {

                    keyPaint = editor.getVCopyKeys(i);
                    keyCtrl = editor.getPaint().getPaintObjects(keyPaint).Tag;
                    copyCtrl = editor.getReport().getControls(keyCtrl);

                    // starting with the first control we move the left
                    // of every control if reach the right margin 
                    // move down a line and restart
                    //
                    offSet = pGetOffsetLeftFromControls(firstCtrlLeft, copyCtrl.getLabel().getAspect().left);
                    left = originalLeft + offSet;

                    if (m_bCopyWithoutMoving) {

                        top = copyCtrl.getLabel().getAspect().top;
                        left = copyCtrl.getLabel().getAspect().left;

                    }

                    if (left - 400 > m_picReport.Width) {
                        left = originalLeft + (offSet % originalLeft);
                        top = top + 100;
                    }

                    if (top > m_picReport.Height) {
                        top = m_picReport.Height - 100;
                    }

                    pAddControlEndAux(left, top, copyCtrl);
                }

                m_copyControlsFromOtherReport = false;

            } 
            else {
                pAddControlEndAux(left, top, null);
            }

            refreshBody();

            return true;
        }

        private Object pGetOffsetLeftFromControls(int leftCtrl1, int leftCtrl2) {
            return leftCtrl2 - leftCtrl1;
        }

        private Object pGetOffsetTopFromControls(int topCtrl1, int topCtrl2) {
            return topCtrl2 - topCtrl1;
        }

        private void pAddControlEndAux(int left, int top, cReportControl baseControl) {
            cReportControl ctrl = null;

            // first we add a control in the main header
            // after the user complete the add operation
            // we would move the control to the desired
            // section line
            //
            ctrl = m_report.getHeaders().item(cGlobals.C_KEY_HEADER).getSectionLines().item(1).getControls().Add;

            // later we will set the properties related to the type of the control
            //
            m_nextNameCtrl = m_nextNameCtrl + 1;
            ctrl.setName(cGlobals.C_CONTROL_NAME + m_nextNameCtrl);

            if (baseControl == null) {
                pSetNewControlProperties(ctrl);
            } 
            else {
                pCopyControl(baseControl, ctrl);
            }

            pSetNewControlPosition(ctrl, left, top);
        }

        private void pCopyFont(cReportFont fromFont, cReportFont toFont) {
            toFont.setBold(fromFont.getBold());
            toFont.setForeColor(fromFont.getForeColor());
            toFont.setItalic(fromFont.getItalic());
            toFont.setName(fromFont.getName());
            toFont.setSize(fromFont.getSize());
            toFont.setStrike(fromFont.getStrike());
            toFont.setUnderLine(fromFont.getUnderline());
        }

        / * TODO: it must be removed
        private void pCopyFontPaint(cReportFont fromFont, CSReportPaint.cReportFont toFont) { 
            toFont.setBold(fromFont.getBold());
            toFont.setForeColor(fromFont.getForeColor());
            toFont.setItalic(fromFont.getItalic());
            toFont.setName(fromFont.getName());
            toFont.setSize(fromFont.getSize());
            toFont.setStrike(fromFont.getStrike());
            toFont.setUnderLine(fromFont.getUnderLine());
        }
         * /

        private void pCopyChart(cReportChart fromChart, cReportChart toChart) {
            toChart.setChartTitle(fromChart.getChartTitle());
            toChart.setChartType(fromChart.getChartType());
            toChart.setDiameter(fromChart.getDiameter());
            toChart.setFormat(fromChart.getFormat());
            toChart.setGridLines(fromChart.getGridLines());
            toChart.setOutlineBars(fromChart.getOutlineBars());
            toChart.setShowValues(fromChart.getShowValues());
            toChart.setThickness(fromChart.getThickness());
            toChart.setTop(fromChart.getTop());
            toChart.setGroupFieldName(fromChart.getGroupFieldName());
            toChart.setGroupFieldIndex(fromChart.getGroupFieldIndex());
            toChart.setGroupValue(fromChart.getGroupValue());
            toChart.setSort(fromChart.getSort());

            cReportChartSerie fromSerie = null;

            for (int _i = 0; _i < fromChart.getSeries().size(); _i++) {
                cReportChartSerie fromSerie = fromChart.getSeries().getItem(_i);
                cReportChartSerie serie = toChart.getSeries().add(null);
                serie.setColor(fromSerie.getColor());
                serie.setLabelFieldName(fromSerie.getLabelFieldName());
                serie.setColor(fromSerie.getLabelIndex());
                serie.setValueFieldName(fromSerie.getValueFieldName());
                serie.setValueIndex(fromSerie.getValueIndex());
            }
        }

        private void pCopyAspect(cReportAspect fromAspect, cReportAspect toAspect) {
            toAspect.setAlign(fromAspect.getAlign());
            toAspect.setBackColor(fromAspect.getBackColor());
            toAspect.setBorderColor(fromAspect.getBorderColor());
            toAspect.setBorderColor3d(fromAspect.getBorderColor3d());
            toAspect.setBorderColor3dShadow(fromAspect.getBorderColor3dShadow());
            toAspect.setBorderType(fromAspect.getBorderType());
            toAspect.setBorderWidth(fromAspect.getBorderWidth());
            toAspect.setCanGrow(fromAspect.getCanGrow());
            toAspect.setFormat(fromAspect.getFormat());
            toAspect.setHeight(fromAspect.getHeight());
            toAspect.setIsAccounting(fromAspect.getIsAccounting());
            toAspect.setLeft(fromAspect.getLeft());
            toAspect.setNZOrder(fromAspect.getNZOrder());
            toAspect.setSelectColor(fromAspect.getSelectColor());
            toAspect.setSymbol(fromAspect.getSymbol());
            toAspect.setTop(fromAspect.getTop());
            toAspect.setTransparent(fromAspect.getTransparent());
            toAspect.setWidth(fromAspect.getWidth());
            toAspect.setWordWrap(fromAspect.getWordWrap());

            pCopyFont(fromAspect.getFont(), toAspect.getFont());
        }

        private void pCopyAspectToPaint(cReportAspect fromAspect, CSReportPaint.cReportAspect toAspect) { 
            toAspect.setAlign(fromAspect.getAlign());
            toAspect.setBackColor(fromAspect.getBackColor());
            toAspect.setBorderColor(fromAspect.getBorderColor());
            toAspect.setBorderColor3d(fromAspect.getBorderColor3d());
            toAspect.setBorderColor3dShadow(fromAspect.getBorderColor3dShadow());
            toAspect.setBorderType(fromAspect.getBorderType());
            toAspect.setBorderWidth(fromAspect.getBorderWidth());
            toAspect.setCanGrow(fromAspect.getCanGrow());
            toAspect.setFormat(fromAspect.getFormat());
            toAspect.setHeight(fromAspect.getHeight());
            toAspect.setIsAccounting(fromAspect.getIsAccounting());
            toAspect.setLeft(fromAspect.getLeft());
            toAspect.setNZOrder(fromAspect.getNZOrder());
            toAspect.setSelectColor(fromAspect.getSelectColor());
            toAspect.setSymbol(fromAspect.getSymbol());
            toAspect.setTop(fromAspect.getTop());
            toAspect.setTransparent(fromAspect.getTransparent());
            toAspect.setWidth(fromAspect.getWidth());
            toAspect.setWordWrap(fromAspect.getWordWrap());

            pCopyFontPaint(fromAspect.getFont(), toAspect.getFont());
        }

        private void pCopyControl(cReportControl fromCtrl, cReportControl toCtrl) { 
            toCtrl.setControlType(fromCtrl.getControlType());

            cReportField field = toCtrl.getField();
            field.fColumns.setFieldType(fromCtrl.getField().getFieldType());
            field.fColumns.setIndex(fromCtrl.getField().getIndex());
            field.setName(fromCtrl.getField().getName());

            toCtrl.getFormulaHide().setName(fromCtrl.getFormulaHide().getName());
            toCtrl.getFormulaHide().setText(fromCtrl.getFormulaHide().getText());
            toCtrl.getFormulaValue().setName(fromCtrl.getFormulaValue().getName());
            toCtrl.getFormulaValue().setText(fromCtrl.getFormulaValue().getText());

            toCtrl.setHasFormulaHide(fromCtrl.getHasFormulaHide());
            toCtrl.setHasFormulaValue(fromCtrl.getHasFormulaValue());

            pCopyAspect(fromCtrl.getImage().getAspect(), toCtrl.getImage().getAspect());

            cReportLabel label = toCtrl.getLabel();
            pCopyAspect(fromCtrl.getLabel().getAspect(), label.getAspect());
            label.setCanGrow(fromCtrl.getLabel().getCanGrow());
            label.setText(fromCtrl.getLabel().getText());

            pCopyAspect(fromCtrl.getLine().getAspect(), toCtrl.getLine().getAspect());
            pCopyChart(fromCtrl.getChart(), toCtrl.getChart());
        }

        private void pSetNewControlProperties(cReportControl ctrl) { 
            ctrl.getLabel().getAspect().setAlign(HorizontalAlignment.Left);

            switch (m_controlType) {
                case csRptEditCtrlType.csRptEditField:
                    ctrl.setControlType(csRptControlType.CSRPTCTFIELD);
                    ctrl.getLabel().setText(m_fieldName);
                    cReportField field = ctrl.getField();
                    field.setIndex(m_fieldIndex);
                    field.setName(m_fieldName);
                    field.setFieldType(m_fieldType);

                    if (cGlobals.isNumberField(m_fieldType)) {
                        cReportAspect aspect = ctrl.getLabel().getAspect();
                        aspect.setAlign(vbRightJustify);
                        aspect.setFormat("#0.00;-#0.00");
                    }
                    break;

                case csRptEditCtrlType.csRptEditFormula:
                    ctrl.setControlType(csRptControlType.CSRPTCTLABEL);
                    ctrl.getFormulaValue().setText(m_formulaText + "(" + m_controlName + ")");
                    ctrl.setHasFormulaValue(true);
                    ctrl.getLabel().getAspect().setFormat("0.00;-0.00");
                    ctrl.getLabel().getAspect().getFont().setBold(true);
                    ctrl.getLabel().setText(ctrl.getFormulaValue().getText());
                    ctrl.getLabel().getAspect().setAlign(vbRightJustify);
                    break;

                case csRptEditCtrlType.csRptEditLabel:
                    ctrl.setControlType(csRptControlType.CSRPTCTLABEL);
                    ctrl.getLabel().setText(m_fieldName);
                    ctrl.getLabel().getAspect().getFont().setBold(true);

                    break;
                case csRptEditCtrlType.csRptEditImage:
                    ctrl.setControlType(csRptControlType.CSRPTCTIMAGE);
                    ctrl.getLabel().setText(m_fieldName);

                    break;
                case csRptEditCtrlType.csRptEditChart:
                    ctrl.setControlType(csRptControlType.CSRPTCTCHART);
                    ctrl.getLabel().setText(m_fieldName);
                    break;
            }

            const int ctrl_height = 285;
            const int ctrl_width = 2000;

            cReportAspect aspect = ctrl.getLabel().getAspect();
            aspect.setWidth(ctrl_width);
            aspect.setHeight(ctrl_height);
            aspect.setTransparent(true);
        }

        private void pSetNewControlPosition(cReportControl ctrl, int left, int top) {
            cReportAspect aspect = ctrl.getLabel().getAspect();
            aspect.setLeft(left);
            aspect.setTop(top);

            CSReportPaint.cReportPaintObject paintObj = null;
            CSReportPaint.csRptPaintObjType paintType = null;

            if (ctrl.getControlType() == csRptControlType.CSRPTCTIMAGE 
                || ctrl.getControlType() == csRptControlType.CSRPTCTCHART) {
                paintType = CSReportPaint.csRptPaintObjType.CSRPTPAINTOBJIMAGE;
            } 
            else {
                paintType = CSReportPaint.csRptPaintObjType.CSRPTPAINTOBJBOX;
            }

            paintObj = m_paint.getNewObject(paintType);

            cReportAspect aspect = null;
            aspect = ctrl.getLabel().getAspect();

            pCopyAspectToPaint(aspect, paintObj.aspect);

            aspect.setLeft(left);
            aspect.setTop(top);

            paintObj.setText(ctrl.getLabel().getText());

            paintObj.setRptType(CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPECONTROL);

            paintObj.setTag(ctrl.getKey());
            ctrl.setKeyPaint(paintObj.getKey());

            // position the control in the desired section line
            //
            moveControl(paintObj.getKey());

            m_paint.drawObject(paintObj.getKey(), m_picReport.hDC, m_picReport);
        }
        
        public void addGroup() {
            mPublic.showGroupProperties(, null, Me);
            refreshAll();
        }

        private cReportGroup pGetGroup(String key) {
            cReportGroup group = null;

            for (int _i = 0; _i < m_report.getGroups().size(); _i++) {
                group = m_report.Groups.getItem(_i);
                if (group.getHeader().getKey() == key) { break; }
                if (group.getFooter().getKey() == key) { break; }
            }

            return group;
        }

        */
        public void addSectionLine() { }
        /*
        public void addSectionLine() {
            cReportSection sec = null;
            CSReportPaint.cReportPaintObject paintObj = null;
            bool isGroup = false;

            sec = pGetSection(isGroup, , , paintObj);

            if (sec == null) { return; }

            switch (sec.getTypeSection()) {

                // in footers we add from top
                // it means that the first section line is the last one
                //
                case csRptTypeSection.CSRPTTPSCFOOTER:
                case csRptTypeSection.CSRPTTPMAINSECTIONFOOTER:

                    cReportAspect aspect = sec.getSectionLines().add(, , 1).getAspect();
                    aspect.setHeight(cGlobals.C_HEIGHT_NEW_SECTION);
                    aspect.setWidth(sec.getAspect().getWidth());

                    // for new sections the top is the top of the previous section
                    // plus cGlobals.C_HEIGHT_NEW_SECTION
                    //
                    aspect.Top = sec.getSectionLines().item(2).getAspect().getTop() - cGlobals.C_HEIGHT_NEW_SECTION;
                    break;

                default:

                    // because the height of the sections is calculated
                    // in pChangeHeightSection which is called by moveSection
                    // which is called by pAddSectionLinesAux, and on this
                    // function, the height of the section line is set with
                    // the result of substract to the height of the section
                    // the sum of every section line except the height of the
                    // last one section line, if we don't modify the height
                    // of the section the new section line will have an height
                    // of zero (actually the minimum height is 1 pixel).
                    //
                    // for this reazon we change the height of the section
                    // but this will fail because the moveSection function
                    // get the height of the section line from the difference
                    // between the height of the section and the new height
                    // which results of moving the section.
                    //
                    // To solve the problem we have this member variable 
                    // which is used to instruct moveSection to add 
                    // to the section height the size of the new section line
                    //
                    m_newSecLineOffSet = cGlobals.C_HEIGHT_NEW_SECTION;

                    cReportAspect aspect = sec.getSectionLines().add().getAspect();
                    aspect.setHeight(cGlobals.C_HEIGHT_NEW_SECTION);
                    aspect.setWidth(sec.getAspect().getWidth());

                    break;
            }

            cReportAspect aspect = sec.getAspect();
            aspect.setHeight(aspect.getHeight() + cGlobals.C_HEIGHT_NEW_SECTION);

            pAddSectionLinesAux(sec, paintObj);

            // we reset this variable to zero
            //
            m_newSecLineOffSet = 0;
        }

        private void pAddSectionLinesAux(
            cReportSection sec, 
            CSReportPaint.cReportPaintObject paintObj) 
        {
            csESectionLineTypes typeSecLn = null;
            int maxBottom = 0;
            int minBottom = 0;
            int index = 0;
            int y = 0;

            switch (sec.getTypeSection()) {
                case csRptTypeSection.CSRPTTPSCHEADER:
                case csRptTypeSection.CSRPTTPMAINSECTIONHEADER:

                    pMoveHeader(sec.getKey(), minBottom, maxBottom, false);
                    cReportAspect aspect = sec.getAspect();
                    y = aspect.getHeight() + aspect.getTop();
                    typeSecLn = csESectionLineTypes.C_KEY_SECLN_HEADER;
                    index = sec.getSectionLines().count() - 1;
                    break;

                case csRptTypeSection.CSRPTTPSCDETAIL:
                case csRptTypeSection.CSRPTTPMAINSECTIONDETAIL:

                    pMoveDetails(sec.getKey(), minBottom, maxBottom, false);
                    cReportAspect aspect = sec.getAspect();
                    y = aspect.getHeight() + aspect.getTop();
                    typeSecLn = csESectionLineTypes.C_KEY_SECLN_DETAIL;
                    index = sec.getSectionLines().count() - 1;
                    break;

                case csRptTypeSection.CSRPTTPGROUPHEADER:

                    pMoveGroupHeader(sec.getKey(), minBottom, maxBottom, false);
                    cReportAspect aspect = sec.getAspect();
                    y = aspect.getHeight() + aspect.getTop();
                    typeSecLn = csESectionLineTypes.C_KEY_SECLN_GROUPH;
                    index = sec.getSectionLines().count() - 1;
                    break;

                case csRptTypeSection.CSRPTTPGROUPFOOTER:

                    pMoveGroupFooter(sec.getKey(), minBottom, maxBottom, false);
                    cReportAspect aspect = sec.getAspect();
                    y = aspect.getHeight() + aspect.getTop();
                    typeSecLn = csESectionLineTypes.C_KEY_SECLN_GROUPF;
                    index = sec.getSectionLines().count() - 1;
                    break;

                case csRptTypeSection.CSRPTTPSCFOOTER:
                case csRptTypeSection.CSRPTTPMAINSECTIONFOOTER:

                    cReportAspect aspect = sec.getAspect();
                    aspect.setTop(aspect.getTop() - cGlobals.C_HEIGHT_NEW_SECTION);
                    pMoveFooter(sec.getKey(), minBottom, maxBottom, false);
                    m_offY = 0;
                    y = aspect.getHeight() + aspect.getTop() - m_offSet - cGlobals.C_HEIGHT_BAR_SECTION;
                    typeSecLn = csESectionLineTypes.C_KEY_SECLN_FOOTER;
                    index = 1;
                    break;
            }
            // we add a paint object to the second to last sectionline 
            // the last sectionline uses the paint object of the section
            //
            cReportSectionLine secL = sec.getSectionLines(index);
            secL.KeyPaint = paintSection(secL.Aspect, 
                                            secL.Key, 
                                            sec.getTypeSection(), 
                                            C_SECTIONLINE + String.valueOf(sec.getSectionLines().count() - 1), 
                                            true);

            CSReportPaint.cReportPaintObject po = m_paint.getPaintSections().item(secL.getKeyPaint());
            po.RptType = typeSecLn;
            po.RptKeySec = sec.getKey();

            CSReportPaint.cReportPaintObject po = m_paint.getPaintSections().item(sec.getKeyPaint());
            po.TextLine = C_SECTIONLINE + sec.getSectionLines().count.ToString();

            moveSection(paintObj, 0, y, minBottom, maxBottom, sec, false);

            refreshBody();
            refreshRule();
        }
        */

        public void addSection(csRptTypeSection typeSection) { }
        /*
        public void addSection(csRptTypeSection typeSection) {

            if (m_editor.getVisible() == false) { return; }

            cReportSection rptSection = null;
            cReportSection topSec = null;
            CSReportPaint.cReportPaintObject paintObj = null;

            int maxBottom = 0;
            int minBottom = 0;
            float y = 0;

            switch (typeSection) {
                case csRptTypeSection.CSRPTTPSCHEADER:
                    cReportSections w_headers = m_report.getHeaders();
                    rptSection = w_headers.Add();
                    rptSection.setName("H_" + ((Integer) rptSection.getIndex()).ToString());
                    cReportAspect aspect = w_headers.item(w_headers.count - 1).getAspect();
                    rptSection.getAspect().setWidth(aspect.getWidth());
                    rptSection.getAspect().setHeight(0);
                    rptSection.getAspect().setTop(aspect.getTop() + aspect.getHeight());

                    rptSection.setKeyPaint(paintSection(rptSection.getAspect(), 
                                                        rptSection.getKey(), 
                                                        CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPESECTIONHEADER, 
                                                        rptSection.getName(), 
                                                        false));

                    cReportAspect w_aspect = rptSection.getAspect();
                    moveSection(m_paint.getPaintObject(rptSection.getKeyPaint()), 
                                0, 
                                w_aspect.getTop(), 
                                w_aspect.getTop() + cGlobals.C_HEIGHT_NEW_SECTION, 
                                w_aspect.getTop() + rptSection.getAspect().getHeight(), 
                                rptSection, 
                                true);
                    break;

                case csRptTypeSection.CSRPTTPSCDETAIL:
                    break;

                case csRptTypeSection.CSRPTTPGROUPHEADER:

                    cIReportGroupSections w_groupsHeaders = m_report.getGroupsHeaders();
                    rptSection = w_groupsHeaders.item(w_groupsHeaders.count);

                    rptSection.setName("GH_" + rptSection.getIndex().ToString());

                    // the first group is next to the last header
                    //
                    if (w_groupsHeaders.count == 1) {
                        topSec = m_report.getHeaders().item(m_report.getHeaders().count());
                    } 
                    else {
                        topSec = w_groupsHeaders.Item(w_groupsHeaders.Count - 1);
                    }

                    cReportAspect w_aspect = w_groupsHeaders.getAspect();
                    rptSection.getAspect().setWidth(w_aspect.getWidth());
                    rptSection.getAspect().setHeight(0);
                    rptSection.getAspect().setTop(w_aspect.getTop() + w_aspect.getHeight());

                    rptSection.setKeyPaint(paintSection(rptSection.getAspect(), 
                                                        rptSection.getKey(), 
                                                        CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEGROUPSECTIONHEADER, 
                                                        rptSection.getName(), 
                                                        false));

                    cReportAspect w_aspect = w_groupsHeaders.getAspect();
                    moveSection(m_paint.getPaintObject(rptSection.getKeyPaint()), 
                                0, 
                                w_aspect.getTop() + cGlobals.C_HEIGHT_NEW_SECTION, 
                                w_aspect.getTop(), 
                                w_aspect.getTop() + cGlobals.C_HEIGHT_NEW_SECTION, 
                                rptSection, 
                                true);
                    break;

                case csRptTypeSection.CSRPTTPGROUPFOOTER:

                    cIReportGroupSections w_groupsFooters = m_report.getGroupsFooters();
                    rptSection = w_groupsFooters.item(1);
                    rptSection.setName("GF_" + rptSection.getIndex().ToString());

                    // all group footers are added to the top so at the
                    // beginning they are next to the detail section
                    //

                    topSec = m_report.getDetails().item(m_report.getDetails().count());

                    cReportAspect w_aspect = w_groupsFooters.getAspect();
                    rptSection.getAspect().setWidth(w_aspect.getWidth());
                    rptSection.getAspect().setHeight(cGlobals.C_HEIGHT_NEW_SECTION);
                    rptSection.getAspect().setTop(w_aspect.getTop() + w_aspect.getHeight());

                    rptSection.setKeyPaint(paintSection(rptSection.getAspect(), 
                                                        rptSection.getKey(), 
                                                        CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEGROUPSECTIONFOOTER, 
                                                        rptSection.getName(), 
                                                        false));

                    paintObj = m_paint.getPaintObject(rptSection.getKeyPaint());
                    pMoveGroupFooter(rptSection.getKey(), minBottom, maxBottom, false);
                    
                    m_offY = 0;

                    cReportAspect w_aspect = w_groupsFooters.getAspect();
                    y = w_aspect.getHeight() + w_aspect.getTop() - cGlobals.C_HEIGHT_BAR_SECTION;

                    moveSection(paintObj, 0, y, minBottom, maxBottom, rptSection, true);
                    break;

                case csRptTypeSection.CSRPTTPSCFOOTER:
                    cReportSections w_footers = m_report.getFooters();

                    // all footers are added to the beginning of the collection
                    // 
                    rptSection = w_footers.add(, , 1);
                    rptSection.setName("F_" + rptSection.getIndex().ToString());

                    cReportAspect aspect = w_footers.item(2).getAspect();
                    rptSection.getAspect().setWidth(aspect.getWidth());
                    rptSection.getAspect().setHeight(cGlobals.C_HEIGHT_NEW_SECTION);
                    rptSection.getAspect().setTop(aspect.getTop());

                    rptSection.setKeyPaint(paintSection(rptSection.getAspect(), 
                                                        rptSection.getKey(), 
                                                        CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPESECTIONFOOTER, 
                                                        rptSection.getName(), 
                                                        false));

                    paintObj = m_paint.getPaintObject(rptSection.getKeyPaint());
                    pMoveFooter(rptSection.getKey(), minBottom, maxBottom, false);

                    m_offY = 0;

                    cReportAspect w_aspect = rptSection.getAspect();
                    y = w_aspect.getHeight() + w_aspect.getTop() - m_offSet - cGlobals.C_HEIGHT_BAR_SECTION;

                    moveSection(paintObj, 0, y, minBottom, maxBottom, rptSection, true);
                    break;
            }

            // every section we add has a section line
            // and we need to set his width
            //
            cReportAspect aspect = rptSection.getSectionLines(1).Aspect;
            aspect.setWidth(rptSection.getAspect().getWidth());

            refreshBody();
            refreshRule();
        }

        public void bringToFront() {
            m_paint.getPaintObjects().zorder(m_keyObj);
            refreshBody();
            m_dataHasChanged = true;
        }

        public void sendToBack() {
            m_paint.getPaintObjects().sendToBack(m_keyObj);
            refreshBody();
            m_dataHasChanged = true;
        }

        public void preview() {
            m_report.getLaunchInfo().setAction(csRptLaunchAction.CSRPTLAUNCHPREVIEW);
            launchReport();
        }

        public void printReport() {
            m_report.getLaunchInfo().setAction(csRptLaunchAction.CSRPTLAUNCHPRINTER);
            launchReport();
        }

        private void launchReport() {
            cMouseWait mouse = new cMouseWait(); 
            try {
                setZOrder();
                showProgressDlg();

                m_report.getLaunchInfo().getPrinter().setPaperInfo(m_report.getPaperInfo());
                m_report.getLaunchInfo().setObjPaint(new CSReportPaint.cReportPrint());
                m_report.getLaunchInfo().setHwnd(Me.hwnd);
                m_report.getLaunchInfo().setShowPrintersDialog(true);
                m_report.launch();

            } catch (Exception ex) {
                cError.mngError(ex, "launchReport", C_MODULE, "");
            }
            finally {
                mouse.Dispose();
                closeProgressDlg();
            }
        }

        public bool saveDocument(bool saveAs) {
            cMouseWait mouse = new cMouseWait(); 
            try {
                bool isNew = false;

                isNew = m_report.getName() == "";

                if (isNew) {
                    m_report.setName(Me.Caption);
                }

                if (saveAs) {
                    isNew = true;
                }

                setZOrder();

                pValidateSectionAspect();

                if (!m_report.save(m_fmain.cmDialog, isNew))
                reLoadReport();
                return true;

            } catch (Exception ex) {
                cError.mngError(ex, "saveDocument", C_MODULE, "");
                return false;
            }
            finally {
                mouse.Dispose();
            }
        }

        private void setZOrder() {
            cReportControl ctrl = null;
            for (int _i = 0; _i < m_report.getControls().size(); _i++) {
                ctrl = m_report.getControls().getItem(_i);
                ctrl.getLabel().getAspect().setNZOrder(m_paint.getPaintObjects().getZOrderForKey(ctrl.getKeyPaint()));
            }
        }

        public void newReport(cReport report) {

            if (report != null) {

                m_report = report;
                reLoadReport();
                pValidateSectionAspect();
                reLoadReport();

            } 
            else {

                m_paint.createPicture(m_picReport);
                refreshRule();

            }

            Me.cNewWizard.show();

            DoEvents;

            mPublic.setDocActive(Me);
        }

        public bool openDocument(String fileName) {
            cMouseWait mouse = new cMouseWait();
            try {

                // to avoid reentrancy
                m_opening = true;

                if (fileName == "") {

                    pSetInitDir();

                    // TODO: the original version of this function
                    //       made a very dirty use of the return of m_report.load
                    //
                    //       the code creates a new document when m_report.load(...)
                    //       return false and m_report.getName != ""
                    //
                    //       when m_report.load is translated we will
                    //       need to rewrite this function
                    //
                    //       in the original function there was a goto to
                    //       a 'done' label 
                    if (!m_report.load(m_fmain.cmDialog)) {

                        if (m_report.getName() == "")  
                            return false;

                        // here the original function has a goto
                        // 'goto done'
                    }

                } 
                else {

                    if (!m_report.loadSilent(fileName)) {
                        return false;
                    }
                }

                reLoadReport();

                // here the original function has a 'done' label
                // 
                / *
                 * 
                Done:
                   With fMain.cmDialog
                        Dim FileEx As CSKernelFile.cFileEx
                        Set FileEx = New CSKernelFile.cFileEx
                        .InitDir = FileEx.FileGetPath(.FileName)
                   End With

                 * I don't know wath this code is suposed to do
                 * but it is clear that is very dirty so we
                 * will need to rewrite all this lines
                 * / 

                Application.DoEvents();

                mPublic.setDocActive(Me);

                m_opening = false;

                return true;
            }
            catch (Exception ex) {
                return false;
            }
            finally {
                mouse.Dispose();
            }            
        }

        public bool saveChanges() {
            csAskEditResult rslt = null;

            if (m_dataHasChanged) {

                rslt = askEdit("Do you want to save changes to " + m_reportFullPath + "?", "CSReportEditor");

                switch (rslt) {
                    case csAskEditResult.csAskRsltYes:
                        if (!saveDocument()) 
                            return false; 
                        break;

                    case csAskEditResult.csAskRsltCancel:
                        return false;
                        break;
                }
            }

            m_dataHasChanged = false;
            return true;
        }

        private csAskEditResult askEdit(String msg, String title) {
            
            DialogResultrslt = MessageBox.Show(
                                    msg, title, 
                                    MessageBoxButtons.YesNoCancel + MessageBoxDefaultButton.Button3, 
                                    MessageBoxIcon.Question);
            switch (rslt) {
                case DialogResult.Yes:
                    return csAskEditResult.csAskRsltYes;
                    break;
                case DialogResult.No:
                    return csAskEditResult.csAskRsltNo;
                    break;
                default:
                    return csAskEditResult.csAskRsltCancel;
                    break;
            }
        }

        private void m_fProperties_ShowHelpDbField(out bool cancel) { 
            int nIndex = 0;
            int nFieldType = 0;
            String sField = "";

            sField = m_fProperties.TxDbField.Text;
            nFieldType = m_fProperties.getFieldType();
            nIndex = m_fProperties.getIndex();

            cancel = !mPublic.showDbFields(sField, nFieldType, nIndex, this);
            if (cancel) { return; }

            m_fProperties.TxDbField.Text = sField;
            m_fProperties.setFieldType(nFieldType);
            m_fProperties.setIndex(nIndex);
            m_fProperties.txText.Text = sField;
        }

        public void showGroupProperties() {
            cReportSection sec = null;
            cReportGroup group = null;
            bool isGroup = false;

            sec = pGetSection(isGroup);

            if (sec == null) { return; }

            if (!isGroup) { return; }

            for (int _i = 0; _i < m_report.getGroups().size(); _i++) {
                Group = m_report.Groups.getItem(_i);
                if (group.getHeader().getKey() == sec.getKey()) { break; }
                if (group.getFooter().getKey() == sec.getKey()) { break; }
            }

            mPublic.showGroupProperties(group, this);

            refreshAll();
        }

        public void moveGroup() {
            cReportSection sec = null;
            cReportGroup group = null;
            bool isGroup = false;

            sec = pGetSection(isGroup);

            if (sec == null) { return; }

            if (!isGroup) { return; }

            for (int _i = 0; _i < m_report.getGroups().size(); _i++) {
                Group = m_report.Groups.getItem(_i);
                if (group.getHeader().getKey() == sec.getKey()) { break; }
                if (group.getFooter().getKey() == sec.getKey()) { break; }
            }

            mPublic.moveGroup(group, this);

            G.redim(m_vSelectedKeys, 0);
            refreshReport();
        }

        public void showSectionProperties() {
            cReportSection sec = null;
            bool isGroup = false;

            sec = pGetSection(isGroup);

            if (sec == null) { return; }

            pShowSecProperties(sec);

            refreshAll();
        }

        public void showSecLnProperties() {
            cReportSection sec = null;
            cReportSectionLine secLn = null;
            bool isSecLn = false;

            sec = pGetSection(, isSecLn, secLn, , true);

            if (sec == null) { return; }
            if (secLn == null) { return; }
            if (!isSecLn) { return; }

            pShowSecProperties(secLn, sec.getName() + ": renglón " + secLn.getIndex().ToString());

            refreshAll();
        }

        private void pShowSecProperties(Object sec, String secLnName) { 
            try {

                m_showingProperties = true;

                if (m_fSecProperties == null) { 
                    m_fSecProperties = new fSecProperties();
                    // TODO: set event handler for ShowEditFormula, UnloadForm
                }

                m_fSecProperties.chkFormulaHide.Value = sec.HasFormulaHide ? vbChecked : vbUnchecked;
                m_fSecProperties.setFormulaHide(sec.getFormulaHide().getText);
                if (sec.GetType() == cReportSection) { m_fSecProperties.TxName.Text = sec.Name; }

                if (sec.GetType() == cReportSectionLine) {
                    m_fSecProperties.LbControl.Caption = secLnName;
                    m_fSecProperties.lbSecLn.Caption = "Line properties:";
                } 
                else {
                    m_fSecProperties.LbControl.Caption = sec.getName();
                }

                m_fSecProperties.Show(vbModal);

                if (m_fSecProperties.getOk()) {
                    if (m_fSecProperties.getSetFormulaHideChanged()) { sec.setHasFormulaHide(m_fSecProperties.chkFormulaHide.Value == vbChecked); }
                    if (m_fSecProperties.getFormulaHideChanged()) { sec.FormulaHide.setText(m_fSecProperties.getFormulaHide()); }
                    if (sec.GetType() == cReportSection) { sec.setName(m_fSecProperties.TxName.Text); }
                }

            } catch (Exception ex) {
                cError.mngError(ex, "pShowSecProperties", C_MODULE, "");
            }
            finally {
                m_fSecProperties.Close();
                m_showingProperties = false;
                m_fSecProperties = null;
            }
        }

        // ReturnSecLn is flag to state that the caller wants to get
        // the section line asociated with the separator of the section
        // remember that the last section line don't have a separator 
        // but share it with the section.
        //
        private cReportSection pGetSection(
            bool isGroup, 
            bool isSecLn, 
            cReportSectionLine secLn, 
            CSReportPaint.cReportPaintObject paintObj, 
            bool returnSecLn, 
            bool isGroupHeader, 
            bool isGroupFooter) 
        {

            cReportSection sec = null;

            isGroup = false;
            isGroupFooter = false;
            isGroupHeader = false;
            isSecLn = false;
            secLn = null;

            if (m_keyFocus == "") { return; }

            // get the section and show his properties
            //
            if (!m_paint.paintObjIsSection(m_keyFocus)) { return; }

            paintObj = m_paint.getPaintSections().item(m_keyFocus);

            // nothing to do
            //
            if (paintObj == null) { return; }

            sec = m_report.getHeaders().item(paintObj.getTag());
            if (sec != null) {

                // it's a header
            } 
            else {
                sec = m_report.getFooters().item(paintObj.getTag());
                if (sec != null) {

                    // it's a footer
                } 
                else {

                    // check if it is a group
                    //
                    sec = m_report.getGroupsHeaders().item(paintObj.getTag());
                    if (sec != null) {

                        // it's a group
                        //
                        isGroup = true;
                        isGroupHeader = true;

                    } 
                    else {
                        sec = m_report.getGroupsFooters().item(paintObj.getTag());
                        if (sec != null) {

                            // it's a group
                            //
                            isGroup = true;
                            isGroupFooter = true;

                        } 
                        else {
                            // check if it is a detail
                            //
                            sec = m_report.getDetails().item(paintObj.getTag());
                            if (sec != null) {

                                // it's a detail

                                // it's a line
                            } 
                            else {
                                isSecLn = true;
                                switch (paintObj.getRptType()) {
                                    case csESectionLineTypes.C_KEY_SECLN_HEADER:
                                        sec = m_report.getHeaders().item(paintObj.getRptKeySec());
                                        break;
                                    case csESectionLineTypes.C_KEY_SECLN_DETAIL:
                                        sec = m_report.getDetails().item(paintObj.getRptKeySec());
                                        break;
                                    case csESectionLineTypes.C_KEY_SECLN_FOOTER:
                                        sec = m_report.getFooters().item(paintObj.getRptKeySec());
                                        break;
                                    case csESectionLineTypes.C_KEY_SECLN_GROUPH:
                                        sec = m_report.getGroupsHeaders().item(paintObj.getRptKeySec());
                                        break;
                                    case csESectionLineTypes.C_KEY_SECLN_GROUPF:
                                        sec = m_report.getGroupsFooters().item(paintObj.getRptKeySec());
                                        break;
                                }
                                secLn = sec.getSectionLines(paintObj.getTag());
                            }
                        }
                    }
                }
            }

            // if the caller wants a section line and the separator
            // is owned by a section (isSecLn == false) we return
            // the last section line of the section asociated to the separator
            //
            if (returnSecLn && !isSecLn) {
                secLn = sec.getSectionLines(sec.getSectionLines().count());
                isSecLn = true;
            }

            return sec;
        }

        public void showProperties() {
            if (m_keyFocus == "") { return; }

            cMouseWait mouse = new cMouseWait();

            if (m_paint.paintObjIsSection(m_keyFocus)) {
                showSectionProperties();
            } 
            else {
                m_keyObj = m_keyFocus;
                pShowCtrlProperties();
            }

            refreshAll();
        }

        private void pShowCtrlProperties() {
            try {

                CSReportPaint.cReportPaintObject paintObject = null;
                cReportControl rptCtrl = null;
                cReportImage image = null;
                bool bMultiSelect = false;
                String sText = "";
                int i = 0;
                
                m_showingProperties = true;

                if (m_fProperties == null) { 
                    m_fProperties = new fProperties();
                    // TODO: set event handler for 
                    // ShowEditFormula, ShowHelpChartField, ShowHelpChartGroupField, ShowHelpDbField
                    // UnloadForm
                }

                paintObject = m_paint.getPaintObject(m_keyObj);
                if (paintObject == null) { return; }

                m_fProperties.txText.Text = paintObject.getText();
                rptCtrl = m_report.getControls().item(paintObject.getTag());

                if (rptCtrl.getControlType() != csRptControlType.CSRPTCTIMAGE) {
                    m_fProperties.hideTabImage();
                }

                if (rptCtrl.getControlType() != csRptControlType.CSRPTCTCHART) {
                    m_fProperties.hideTabChart();
                } 
                else {

                    cUtil.listSetListIndexForId(m_fProperties.cbType, rptCtrl.getChart().getChartType());
                    cUtil.listSetListIndexForId(m_fProperties.cbFormatType, rptCtrl.getChart().getFormat());
                    cUtil.listSetListIndexForId(m_fProperties.cbChartSize, rptCtrl.getChart().getDiameter());
                    cUtil.listSetListIndexForId(m_fProperties.cbChartThickness, rptCtrl.getChart().getThickness());
                    cUtil.listSetListIndexForId(m_fProperties.cbLinesType, rptCtrl.getChart().getGridLines());

                    m_fProperties.txChartTop.Text = rptCtrl.getChart().getTop();

                    m_fProperties.TxDbFieldGroupValue.Text = rptCtrl.getChart().getGroupFieldName();
                    m_fProperties.setChartGroupIndex(rptCtrl.getChart().getGroupFieldIndex());
                    m_fProperties.txChartGroupValue.Text = rptCtrl.getChart().getGroupValue();

                    if (rptCtrl.getChart().getOutlineBars()) {
                        m_fProperties.opLinesYes.Value = true;
                    } 
                    else {
                        m_fProperties.opLinesNo.Value = true;
                    }

                    if (rptCtrl.getChart().getShowValues()) {
                        m_fProperties.opValuesYes.Value = true;
                    } 
                    else {
                        m_fProperties.opValuesNo.Value = true;
                    }

                    m_fProperties.chkSort.Value = rptCtrl.getChart().getSort() ? vbChecked : vbUnchecked;

                    m_fProperties.txText.Text = rptCtrl.getChart().getChartTitle();

                    if (rptCtrl.getChart().getSeries().count()) {
                        m_fProperties.TxDbFieldLbl1.Text = rptCtrl.getChart().getSeries(1).getLabelFieldName();
                        m_fProperties.TxDbFieldVal1.Text = rptCtrl.getChart().getSeries(1).getValueFieldName();

                        m_fProperties.setChartIndex(0, rptCtrl.getChart().getSeries(1).getLabelIndex());
                        m_fProperties.setChartIndex(1, rptCtrl.getChart().getSeries(1).tetValueIndex());

                        cUtil.listSetListIndexForId(m_fProperties.cbColorSerie1, rptCtrl.getChart().getSeries(1).getColor());

                        if (rptCtrl.getChart().getSeries().count() > 1) {
                            m_fProperties.TxDbFieldLbl2.Text = rptCtrl.getChart().getSeries(2).getLabelFieldName();
                            m_fProperties.TxDbFieldVal2.Text = rptCtrl.getChart().getSeries(2).getValueFieldName();

                            // Decidi no implementarlo ya que no lo encontre util
                            //
                            //m_fProperties.ChartFieldType(2) =
                            //m_fProperties.ChartFieldType(3) =
                            m_fProperties.setChartIndex(2, rptCtrl.getChart().getSeries(2).getLabelIndex());
                            m_fProperties.setChartIndex(3, rptCtrl.getChart().getSeries(2).getValueIndex());

                            cUtil.listSetListIndexForId(m_fProperties.cbColorSerie2, rptCtrl.getChart().getSeries(2).getColor());
                        }
                    }
                }

                if (rptCtrl.getControlType() == csRptControlType.CSRPTCTFIELD 
                    || rptCtrl.getControlType() == csRptControlType.CSRPTCTDBIMAGE) {
                    m_fProperties.txText.Enabled = false;
                    cReportField w_field = rptCtrl.getField();
                    m_fProperties.txText.Text = w_field.getName();
                    m_fProperties.TxDbField.Text = w_field.getName();
                    m_fProperties.setFieldType(w_field.fColumns.getFieldType());
                    m_fProperties.setIndex(w_field.fColumns.getIndex());
                } 
                else {
                    m_fProperties.hideTabField();
                    m_fProperties.txText.Enabled = true;
                }

                m_fProperties.TxName.Text = rptCtrl.getName();
                m_fProperties.LbControl.Caption = rptCtrl.getName();
                m_fProperties.chkFormulaHide.Value = rptCtrl.getHasFormulaHide() ? vbChecked : vbUnchecked;
                m_fProperties.chkFormulaValue.Value = rptCtrl.getHasFormulaValue() ? vbChecked : vbUnchecked;

                m_fProperties.txExportColIdx.Text = rptCtrl.getExportColIdx();
                m_fProperties.chkIsFreeCtrl = rptCtrl.getIsFreeCtrl() ? vbChecked : vbUnchecked;

                m_fProperties.txTag.Text = rptCtrl.getTag();
                m_fProperties.setFormulaHide(rptCtrl.getFormulaHide().getText());
                m_fProperties.setFormulaValue(rptCtrl.getFormulaValue().getText());
                m_fProperties.txIdxGroup.csValue = rptCtrl.getFormulaValue().getIdxGroup();
                m_fProperties.opBeforePrint.Value = rptCtrl.getFormulaValue().getWhenEval() == csRptWhenEval.CSRPTEVALPRE;
                m_fProperties.opAfterPrint.Value = rptCtrl.getFormulaValue().getWhenEval() == csRptWhenEval.CSRPTEVALPOST;

                cReportAspect w_aspect = rptCtrl.getAspect();
                m_fProperties.chkCanGrow.Value = w_aspect.getCanGrow() ? vbChecked : vbUnchecked;
                m_fProperties.TxFormat.Text = w_aspect.getFormat();
                m_fProperties.txSymbol.Text = w_aspect.getfFormat().getSymbol();
                m_fProperties.setIsAccounting(w_aspect.getfFormat().getIsAccounting());
                m_fProperties.chkWordWrap.Value = w_aspect.getWordWrap() ? vbChecked : vbUnchecked;

                cUtil.listSetListIndexForId(m_fProperties.cbAlign, w_aspect.getAlign());

                m_fProperties.txBorderColor.Text = w_aspect.getBorderColor();
                m_fProperties.txBorder3D.Text = w_aspect.getBorderColor3d();
                m_fProperties.txBorderShadow.Text = w_aspect.getBorderColor3dShadow();
                m_fProperties.chkBorderRounded.Value = w_aspect.getBorderRounded() ? vbChecked : vbUnchecked;
                m_fProperties.txBorderWidth.Text = w_aspect.getBorderWidth();

                cUtil.listSetListIndexForId(m_fProperties.cbBorderType, w_aspect.getBorderType());

                cReportFont w_font = w_aspect.getFont();
                m_fProperties.txFont.Text = w_font.getName();
                m_fProperties.TxForeColor.Text = w_font.getForeColor();
                m_fProperties.TxFontSize.Text = w_font.getSize();
                m_fProperties.chkFontBold.Value = w_font.getBold() ? vbChecked : vbUnchecked;
                m_fProperties.chkFontItalic.Value = w_font.getItalic() ? vbChecked : vbUnchecked;
                m_fProperties.chkFontUnderline.Value = w_font.getUnderline() ? vbChecked : vbUnchecked;
                m_fProperties.chkFontStrike.Value = w_font.getStrike() ? vbChecked : vbUnchecked;

                cReportAspect w_aspect = paintObject.getAspect();
                m_fProperties.txLeft.Text = w_aspect.getLeft();
                m_fProperties.txTop.Text = w_aspect.getTop();
                m_fProperties.txWidth.Text = w_aspect.getWidth();
                m_fProperties.txHeight.Text = w_aspect.getHeight();
                m_fProperties.TxBackColor.Text = w_aspect.getBackColor();
                m_fProperties.chkTransparent.Value = w_aspect.getTransparent() ? vbChecked : vbUnchecked;

                bMultiSelect = m_vSelectedKeys.length > 1;

                m_fProperties.resetChangedFlags();

                m_fProperties.Show(vbModal);

                if (!m_fProperties.getOk()) { return; }

                for (i = 1; i <= m_vSelectedKeys.length; i++) {

                    paintObject = m_paint.getPaintObject(m_vSelectedKeys[i]);
                    rptCtrl = m_report.getControls().item(paintObject.getTag());

                    if (!bMultiSelect) {
                        if (rptCtrl.getName() != m_fProperties.TxName.Text) {
                            if (rptCtrl.getName() != "") {
                                if (cWindow.ask("You have changed the name of this control.;;Do you want to update all references to this control in all formulas of this report?", VbMsgBoxResult.vbYes)) {
                                    pUpdateFormulas(rptCtrl.getName(), m_fProperties.TxName.Text);
                                }
                            }
                        }
                        rptCtrl.setName(m_fProperties.TxName.Text);
                    }

                    if (m_fProperties.getTextChanged()) { rptCtrl.getLabel().setText(m_fProperties.txText.Text); }
                    if (m_fProperties.getTagChanged()) { rptCtrl.setTag(m_fProperties.txTag.Text); }
                    if (m_fProperties.getSetFormulaHideChanged()) { rptCtrl.setHasFormulaHide(m_fProperties.chkFormulaHide.Value == vbChecked); }
                    if (m_fProperties.getSetFormulaValueChanged()) { rptCtrl.setHasFormulaValue(m_fProperties.chkFormulaValue.Value == vbChecked); }
                    if (m_fProperties.getFormulaHideChanged()) { rptCtrl.getFormulaHide().setText(m_fProperties.getFormulaHide()); }
                    if (m_fProperties.getFormulaValueChanged()) { rptCtrl.getFormulaValue().setText(m_fProperties.getFormulaValue()); }
                    if (m_fProperties.getIdxGroupChanged()) { rptCtrl.getFormulaValue().setIdxGroup(m_fProperties.txIdxGroup.csValue); }
                    if (m_fProperties.getWhenEvalChanged()) { rptCtrl.getFormulaValue().setWhenEval(m_fProperties.opAfterPrint.Value ? csRptWhenEval.CSRPTEVALPOST : csRptWhenEval.CSRPTEVALPRE)); }

                    if (m_fProperties.getExportColIdxChanged()) { rptCtrl.setExportColIdx(m_fProperties.txExportColIdx.Text); }
                    if (m_fProperties.getIsFreeCtrlChanged()) { rptCtrl.setIsFreeCtrl(m_fProperties.chkIsFreeCtrl.Value == vbChecked); }

                    if (rptCtrl.getControlType() == csRptControlType.CSRPTCTFIELD || rptCtrl.getControlType() == csRptControlType.CSRPTCTDBIMAGE) {

                        cReportField w_field = rptCtrl.getField();
                        if (m_fProperties.getDbFieldChanged()) {
                            w_field.setFieldType(m_fProperties.getFieldType());
                            w_field.setIndex(m_fProperties.getIndex());
                            w_field.setName(m_fProperties.TxDbField.Text);
                        }
                    }

                    if (m_fProperties.getPictureChanged()) {
                        image = rptCtrl.image;
                        __TYPE_NOT_FOUND w___TYPE_NOT_FOUND = m_fProperties.picImage;
                        int width = 0;
                        int height = 0;

                        width = w___TYPE_NOT_FOUND.ScaleX(w___TYPE_NOT_FOUND.Picture.width, vbHimetric, vbTwips);
                        height = w___TYPE_NOT_FOUND.ScaleY(w___TYPE_NOT_FOUND.Picture.height, vbHimetric, vbTwips);

                        image.setHImage(m_paint.copyBitmap(w___TYPE_NOT_FOUND.hDC, width, height, image.getHImage()));
                    }

                    if (rptCtrl.getControlType() == csRptControlType.CSRPTCTCHART) {

                        if (rptCtrl.getChart().getSeries().count() < 1) { rptCtrl.getChart().getSeries().add(null); }

                        if (m_fProperties.getChartTypeChanged()) {
                            rptCtrl.getChart().setChartType(ListID(m_fProperties.cbType));
                        }
                        if (m_fProperties.getChartFormatTypeChanged()) {
                            rptCtrl.getChart().setFormat(ListID(m_fProperties.cbFormatType));
                        }
                        if (m_fProperties.getChartSizeChanged()) {
                            rptCtrl.getChart().setDiameter(ListID(m_fProperties.cbChartSize));
                        }
                        if (m_fProperties.getChartThicknessChanged()) {
                            rptCtrl.getChart().setThickness(ListID(m_fProperties.cbChartThickness));
                        }
                        if (m_fProperties.getChartLinesTypeChanged()) {
                            rptCtrl.getChart().setGridLines(ListID(m_fProperties.cbLinesType));
                        }

                        if (m_fProperties.getChartShowLinesChanged()) {
                            rptCtrl.getChart().setOutlineBars(m_fProperties.opLinesYes.Value);
                        }
                        if (m_fProperties.getChartShowValuesChanged()) {
                            rptCtrl.getChart().setShowValues(m_fProperties.opValuesYes.Value);
                        }

                        if (m_fProperties.getTextChanged()) {
                            rptCtrl.getChart().setChartTitle(m_fProperties.txText.Text);
                        }

                        if (m_fProperties.getChartTopChanged()) {
                            rptCtrl.getChart().setTop(m_fProperties.txChartTop.csValue);
                        }

                        if (m_fProperties.getChartSortChanged()) {
                            rptCtrl.getChart().setSort(m_fProperties.chkSort.Value == vbChecked);
                        }

                        if (m_fProperties.getChartGroupValueChanged()) {
                            rptCtrl.getChart().setGroupValue(m_fProperties.txChartGroupValue.csValue);
                        }

                        if (m_fProperties.getChartFieldGroupChanged()) {
                            rptCtrl.getChart().setGroupFieldName(m_fProperties.TxDbFieldGroupValue.Text);
                            rptCtrl.getChart().setGroupFieldIndex(m_fProperties.getChartGroupIndex());
                        }

                        if (m_fProperties.getChartFieldLbl1Changed()) {
                            rptCtrl.getChart().getSeries(1).LabelFieldName = m_fProperties.TxDbFieldLbl1.Text;
                            rptCtrl.getChart().getSeries(1).LabelIndex = m_fProperties.getChartIndex(0);
                        }
                        if (m_fProperties.getChartFieldVal1Changed()) {
                            rptCtrl.getChart().getSeries(1).ValueFieldName = m_fProperties.TxDbFieldVal1.Text;
                            rptCtrl.getChart().getSeries(1).ValueIndex = m_fProperties.getChartIndex(1);
                        }

                        if (m_fProperties.getChartColorSerie1Changed()) {
                            rptCtrl.getChart().getSeries(1).Color = ListID(m_fProperties.cbColorSerie1);
                        }

                        if (m_fProperties.getChartFieldLbl2Changed() || m_fProperties.getChartFieldVal2Changed()) {
                            if (rptCtrl.getChart().getSeries().count() < 2) { rptCtrl.getChart().getSeries().add(null); }
                        }

                        if (m_fProperties.TxDbFieldLbl2.Text == "" || m_fProperties.TxDbFieldVal2.Text == "") {
                            if (rptCtrl.getChart().getSeries().count() > 1) { rptCtrl.getChart().getSeries().remove(2); }
                        }

                        if (rptCtrl.getChart().getSeries().count() > 1) {

                            if (m_fProperties.getChartFieldLbl2Changed()) {
                                rptCtrl.getChart().getSeries(2).LabelFieldName = m_fProperties.TxDbFieldLbl2.Text;
                                rptCtrl.getChart().getSeries(2).LabelIndex = m_fProperties.getChartIndex(2);
                            }
                            if (m_fProperties.getChartFieldVal2Changed()) {
                                rptCtrl.getChart().getSeries(2).ValueFieldName = m_fProperties.TxDbFieldVal2.Text;
                                rptCtrl.getChart().getSeries(2).ValueIndex = m_fProperties.getChartIndex(3);
                            }

                            if (m_fProperties.getChartColorSerie2Changed()) {
                                rptCtrl.getChart().getSeries(2).Color = ListID(m_fProperties.cbColorSerie2);
                            }
                        }
                    }

                    if (m_fProperties.getTextChanged()) { paintObject.setText(m_fProperties.txText.Text); }

                    cReportAspect w_aspect = rptCtrl.getLabel().getAspect();
                    if (m_fProperties.getLeftChanged()) { w_aspect.setLeft(Double.parseDouble(m_fProperties.txLeft.Text)); }
                    if (m_fProperties.getTopChanged()) { w_aspect.setTop(Double.parseDouble(m_fProperties.txTop.Text)); }
                    if (m_fProperties.getWidthChanged()) { w_aspect.setWidth(Double.parseDouble(m_fProperties.txWidth.Text)); }
                    if (m_fProperties.getHeightChanged()) { w_aspect.setHeight(Double.parseDouble(m_fProperties.txHeight.Text)); }
                    if (m_fProperties.getBackColorChanged()) { w_aspect.setBackColor(Long.parseLong(m_fProperties.TxBackColor.Text)); }
                    if (m_fProperties.getTransparentChanged()) { w_aspect.setTransparent(m_fProperties.chkTransparent.Value == vbChecked); }
                    if (m_fProperties.getAlignChanged()) { w_aspect.setAlign(ListID(m_fProperties.cbAlign)); }
                    if (m_fProperties.getFormatChanged()) { w_aspect.setFormat(m_fProperties.TxFormat.Text); }
                    if (m_fProperties.getSymbolChanged()) {
                        w_aspect.setSymbol(m_fProperties.txSymbol.Text);
                        w_aspect.setIsAccounting(m_fProperties.getIsAccounting());
                    }
                    if (m_fProperties.getWordWrapChanged()) { w_aspect.setWordWrap(m_fProperties.chkWordWrap.Value == vbChecked); }
                    if (m_fProperties.getCanGrowChanged()) { w_aspect.setCanGrow(m_fProperties.chkCanGrow.Value == vbChecked); }

                    if (m_fProperties.getBorderColorChanged()) { w_aspect.setBorderColor(Long.parseLong(m_fProperties.txBorderColor.Text)); }
                    if (m_fProperties.getBorder3DChanged()) { w_aspect.setBorderColor3d(Long.parseLong(m_fProperties.txBorder3D.Text)); }
                    if (m_fProperties.getBorder3DShadowChanged()) { w_aspect.setBorderColor3dShadow(Long.parseLong(m_fProperties.txBorderShadow.Text)); }
                    if (m_fProperties.getBorderRoundedChanged()) { w_aspect.setBorderRounded(m_fProperties.chkBorderRounded.Value == vbChecked); }
                    if (m_fProperties.getBorderWidthChanged()) { w_aspect.setBorderWidth(Long.parseLong(m_fProperties.txBorderWidth.Text)); }
                    if (m_fProperties.getBorderTypeChanged()) { w_aspect.setBorderType(ListID(m_fProperties.cbBorderType)); }

                    cReportFont w_font = w_aspect.getFont();
                    if (m_fProperties.getFontChanged()) { w_font.setName(m_fProperties.txFont.Text); }
                    if (m_fProperties.getForeColorChanged()) { w_font.setForeColor(Long.parseLong(m_fProperties.TxForeColor.Text)); }
                    if (m_fProperties.getFontSizeChanged()) { w_font.setSize(Double.parseDouble(m_fProperties.TxFontSize.Text)); }
                    if (m_fProperties.getBoldChanged()) { w_font.setBold(m_fProperties.chkFontBold.Value == vbChecked); }
                    if (m_fProperties.getItalicChanged()) { w_font.setItalic(m_fProperties.chkFontItalic.Value == vbChecked); }
                    if (m_fProperties.getUnderlineChanged()) { w_font.setUnderLine(m_fProperties.chkFontUnderline.Value == vbChecked); }
                    if (m_fProperties.getStrikeChanged()) { w_font.setStrike(m_fProperties.chkFontStrike.Value == vbChecked); }

                    if (m_fProperties.getPictureChanged()) {
                        paintObject.setHImage(rptCtrl.getImage().getHImage());
                    }

                    //////////////////////////////////////////////////////////'
                    // esto sera removido cuando agreguemos una interface     '
                    //////////////////////////////////////////////////////////'

                    cReportAspect w_aspect = paintObject.getAspect();
                    if (m_fProperties.getLeftChanged()) { w_aspect.setLeft(Double.parseDouble(m_fProperties.txLeft.Text)); }
                    if (m_fProperties.getTopChanged()) { w_aspect.setTop(Double.parseDouble(m_fProperties.txTop.Text)); }
                    if (m_fProperties.getWidthChanged()) { w_aspect.setWidth(Double.parseDouble(m_fProperties.txWidth.Text)); }
                    if (m_fProperties.getHeightChanged()) { w_aspect.setHeight(Double.parseDouble(m_fProperties.txHeight.Text)); }
                    if (m_fProperties.getBackColorChanged()) { w_aspect.setBackColor(Long.parseLong(m_fProperties.TxBackColor.Text)); }
                    if (m_fProperties.getTransparentChanged()) { w_aspect.setTransparent(m_fProperties.chkTransparent.Value == vbChecked); }
                    if (m_fProperties.getAlignChanged()) { w_aspect.setAlign(ListID(m_fProperties.cbAlign)); }
                    if (m_fProperties.getFormatChanged()) { w_aspect.setFormat(m_fProperties.TxFormat.Text); }
                    if (m_fProperties.getSymbolChanged()) { w_aspect.setSymbol(m_fProperties.txSymbol.Text); }
                    if (m_fProperties.getWordWrapChanged()) { w_aspect.setWordWrap(m_fProperties.chkWordWrap.Value == vbChecked); }

                    if (m_fProperties.getBorderTypeChanged()) { w_aspect.setBorderType(ListID(m_fProperties.cbBorderType)); }

                    if (w_aspect.getBorderType() == CSRPTBSNONE) {
                        w_aspect.setBorderColor(vbBlack);
                        w_aspect.setBorderWidth(1);
                        w_aspect.setBorderRounded(false);
                        w_aspect.setBorderType(CSRPTBSFIXED);
                    } 
                    else {
                        if (m_fProperties.getBorderColorChanged()) { w_aspect.setBorderColor(Long.parseLong(m_fProperties.txBorderColor.Text)); }
                        if (m_fProperties.getBorder3DChanged()) { w_aspect.setBorderColor3d(Long.parseLong(m_fProperties.txBorder3D.Text)); }
                        if (m_fProperties.getBorder3DShadowChanged()) { w_aspect.setBorderColor3dShadow(Long.parseLong(m_fProperties.txBorderShadow.Text)); }
                        if (m_fProperties.getBorderRoundedChanged()) { w_aspect.setBorderRounded(m_fProperties.chkBorderRounded.Value == vbChecked); }
                        if (m_fProperties.getBorderWidthChanged()) { w_aspect.setBorderWidth(Long.parseLong(m_fProperties.txBorderWidth.Text)); }
                    }

                    cReportFont w_font = w_aspect.getFont();
                    if (m_fProperties.getFontChanged()) { w_font.setName(m_fProperties.txFont.Text); }
                    if (m_fProperties.getForeColorChanged()) { w_font.setForeColor(Long.parseLong(m_fProperties.TxForeColor.Text)); }
                    if (m_fProperties.getFontSizeChanged()) { w_font.setSize(Double.parseDouble(m_fProperties.TxFontSize.Text)); }
                    if (m_fProperties.getBoldChanged()) { w_font.setBold(m_fProperties.chkFontBold.Value == vbChecked); }
                    if (m_fProperties.getItalicChanged()) { w_font.setItalic(m_fProperties.chkFontItalic.Value == vbChecked); }
                    if (m_fProperties.getUnderlineChanged()) { w_font.setUnderLine(m_fProperties.chkFontUnderline.Value == vbChecked); }
                    if (m_fProperties.getStrikeChanged()) { w_font.setStrike(m_fProperties.chkFontStrike.Value == vbChecked); }
                }

                m_dataHasChanged = true;

            } catch (Exception ex) {
                cError.mngError(ex, "pShowCtrlProperties", C_MODULE, "");
            }
            finally {
                m_fProperties.Hide();
                m_showingProperties = false;
                m_fProperties = null;
                m_paint.endMove(m_picReport);
            }
        }

        private void beginDraging() {
            m_picReport.SetFocus;
            m_draging = true;
            m_picReport.MousePointer = vbCustom;
            m_picReport.MouseIcon = LoadPicture(App.Path + "\\move32x32.cur");
        }

        private void endDraging() {
            m_draging = false;
            m_controlType = csRptEditCtrlType.csRptEditNone;
            m_picReport.MousePointer = vbDefault;
        }

        public void showToolBox() {
            DoEvents;

            m_fToolBox = mPublic.getToolBox(this);

            mPublic.clearToolBox(this);

            pAddColumnsToToolbox(m_report.getConnect().getDataSource(), m_report.getConnect().getColumns());
            cReportConnect connect = null;

            for (int _i = 0; _i < m_report.getConnectsAux().size(); _i++) {
                Connect = m_Report.ConnectsAux.getItem(_i);
                pAddColumnsToToolbox(connect.getDataSource(), connect.getColumns());
            }

            cReportControl ctrl = null;

            for (int _i = 0; _i < m_report.getControls().size(); _i++) {
                Ctrl = m_Report.Controls.getItem(_i);
                if (mPublic.isNumberField(ctrl.getField().getFieldType())) {
                    m_fToolBox.addLbFormula(ctrl.getField().getName());

                    // Esto no es lo mas prolijo pero
                    // tomando en cuenta los tiempos
                    // por ahora queda asi. Lo correcto
                    // seria establecer algun mecanismo
                    // para indicar en la definicion de la
                    // formula que puede ser propuesta en
                    // el toolbox.
                    m_fToolBox.addFormula("Suma", ctrl.getName(), "_Sum");
                    m_fToolBox.addFormula("Máximo", ctrl.getName(), "_Max");
                    m_fToolBox.addFormula("Minimo", ctrl.getName(), "_Min");
                    m_fToolBox.addFormula("Promedio", ctrl.getName(), "_Average");
                }
            }

            m_fToolBox.init();
            m_fToolBox.Show(vbModeless, fMain);
        }

        public void pAddColumnsToToolbox(String dataSource, cColumnsInfo columns) { 
            cColumnInfo col = null;

            for (int _i = 0; _i < columns.size(); _i++) {
                Col = Columns.getItem(_i);
                m_fToolBox.addField(mPublic.getDataSourceStr(dataSource) + col.getName(), 
                                        CInt(col.getTypeColumn()), 
                                        col.getPosition());
                m_fToolBox.addLabels(col.getName());
            }
        }

        public void copy() {
            try {
                int i = 0;

                if (m_vSelectedKeys.Length == 0) { return; }

                G.redim(m_vCopyKeys, m_vSelectedKeys.Length);

                for (i = 1; i <= m_vSelectedKeys.Length; i++) {
                    m_vCopyKeys[i] = m_vSelectedKeys[i];
                }

                fMain.setReportCopySource(this);

            } catch (Exception ex) {
                cError.mngError(ex, "Copy", C_MODULE, "");
            }
        }

        public void paste(bool bDontMove) {
            try {

                m_bCopyWithoutMoving = bDontMove;

                if (m_vCopyKeys.Length == 0) {

                    if (fMain.getReportCopySource() == null) { return; }

                    m_copyControlsFromOtherReport = true;

                } 
                else {

                    m_copyControls = true;

                }

                fFormula.addLabel();

            } catch (Exception ex) {
                cError.mngError(ex, "Paste", C_MODULE, "");
            }
        }

        public void editText() {
            try {

                const int c_margen = 20;

                String sText = "";
                CSReportPaint.cReportAspect paintObjAspect = null;
                cReportControl ctrl = null;

                if (m_keyObj == "") { return; }

                cReportPaintObject w_getPaintObject = m_paint.getPaintObject(m_keyObj);
                paintObjAspect = w_getPaintObject.getAspect();
                sText = w_getPaintObject.getText();
                ctrl = m_report.getControls().item(w_getPaintObject.getTag());
                if (paintObjAspect == null) { return; }

                TxEdit.Text = sText;
                TxEdit.Left = paintObjAspect.getLeft() + c_margen;
                TxEdit.Top = paintObjAspect.getTop() + c_margen - paintObjAspect.getOffset();
                TxEdit.Width = paintObjAspect.getWidth() - c_margen * 2;
                TxEdit.Height = paintObjAspect.getHeight() - c_margen * 2;
                TxEdit.Visible = true;
                TxEdit.ZOrder;
                TxEdit.SetFocus;
                TxEdit.FontName = paintObjAspect.getFont().getName();
                TxEdit.FontSize = paintObjAspect.getFont().getSize();
                TxEdit.FontBold = paintObjAspect.getFont().getBold();
                TxEdit.ForeColor = paintObjAspect.getFont().getForeColor();
                TxEdit.BackColor = paintObjAspect.getBackColor();

            } catch (Exception ex) {
                cError.mngError(ex, "EditText", C_MODULE, "");
            }
        }

        private void endEditText(bool descartar) {
            if (!TxEdit.Visible) { return; }

            TxEdit.Visible = false;

            if (descartar) { return; }

            m_dataHasChanged = true;

            CSReportPaint.cReportPaintObject paintObjAspect = null;
            paintObjAspect = m_paint.getPaintObject(m_keyObj);
            if (paintObjAspect == null) { return; }

            String sKeyRpt = "";
            sKeyRpt = paintObjAspect.getTag();

            paintObjAspect.setText(TxEdit.Text);

            m_report.getControls().item(sKeyRpt).getLabel().setText(paintObjAspect.getText());
            refreshBody();
        }

        private object paintStandarSections() {
            cReportPaintObject paintSec = null;

            cReportSections w_headers = m_report.getHeaders();
            cReportSection w_item = w_headers.item(cGlobals.C_KEY_HEADER);
            
            w_item.setKeyPaint(paintSection(m_report.getHeaders().item(cGlobals.C_KEY_HEADER).getAspect(), 
                                            cGlobals.C_KEY_HEADER, 
                                            CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEMAINSECTIONHEADER, 
                                            "Header 1", false));
            
            paintSec = m_paint.getPaintSections(w_item.getKeyPaint());
            paintSec.setHeightSec(w_item.getAspect().getHeight());

            pAddPaintSetcionForSecLn(w_headers.item(cGlobals.C_KEY_HEADER), 
                                                    csESectionLineTypes.C_KEY_SECLN_HEADER);

            cReportSections w_details = m_report.getDetails();
            cReportSection w_item = w_details.item(cGlobals.C_KEY_DETAIL);
            
            w_item.setKeyPaint(paintSection(m_report.getDetails().item(C_KEY_DETAIL).getAspect(), 
                                            cGlobals.C_KEY_DETAIL, 
                                            CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEMAINSECTIONDETAIL, 
                                            "Detail", false));
            
            paintSec = m_paint.getPaintSections(w_item.getKeyPaint());
            paintSec.setHeightSec(w_item.getAspect().getHeight());
            
            pAddPaintSetcionForSecLn(w_details.item(cGlobals.C_KEY_DETAIL), 
                                        csESectionLineTypes.C_KEY_SECLN_DETAIL);

            cReportSections w_footers = m_report.getFooters();
            cReportSection w_item = w_footers.item(C_KEY_FOOTER);

            w_item.setKeyPaint(paintSection(m_report.getFooters().item(cGlobals.C_KEY_FOOTER).getAspect(), 
                                            cGlobals.C_KEY_FOOTER, 
                                            CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEMAINSECTIONFOOTER, 
                                            "Footer 1", false));

            paintSec = m_paint.getPaintSections(w_item.getKeyPaint());
            paintSec.setHeightSec(w_item.getAspect().getHeight());
            pAddPaintSetcionForSecLn(w_footers.item(C_KEY_FOOTER), C_KEY_SECLN_FOOTER);
        }

        private String paintSection(cReportAspect aspect, 
                                    String sKey, 
                                    CSReportPaint.csRptPaintRptType rptType, 
                                    String text, 
                                    bool isSectionLine) 
        { 

            CSReportPaint.cReportPaintObject paintObj = null;
            paintObj = m_paint.getNewSection(CSReportPaint.csRptPaintObjType.CSRPTPAINTOBJBOX);

            cReportAspect w_aspect = paintObj.getAspect();

            // we only draw the bottom line of the sections
            //
            w_aspect.setLeft(0);
            w_aspect.setTop(aspect.getTop() + aspect.getHeight() - cGlobals.C_HEIGHT_BAR_SECTION);
            w_aspect.setWidth(aspect.getWidth());
            w_aspect.setHeight(cGlobals.C_HEIGHT_BAR_SECTION);

            int innerColor = 0;
            innerColor = &HAEAEAE;

            if (isSectionLine) {
                w_aspect.setBackColor(innerColor);
                w_aspect.setBorderColor(vbRed);
            } 
            else {
                if (rptType == CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEGROUPSECTIONFOOTER 
                    || rptType == CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEGROUPSECTIONHEADER) {
                    w_aspect.setBackColor(innerColor);
                    w_aspect.setBorderColor(&HC0C000);
                } 
                else {
                    w_aspect.setBackColor(innerColor);
                    w_aspect.setBorderColor(&H5A7FB);
                }
            }

            if (rptType == CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPEMAINSECTIONFOOTER 
                || rptType == CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPESECTIONFOOTER) {
                w_aspect.setOffset(m_offSet);
            }

            paintObj.setIsSection(!isSectionLine);

            paintObj.setRptType(rptType);
            paintObj.setTag(sKey);

            paintObj.setText(text);

            return paintObj.getKey();
        }

        private bool getLineRegionForControl(String sKeyPaintObj, 
                                                out cReportSectionLine rptSecLine, 
                                                bool isFreeCtrl) 
        { 
            cReportSection rptSection = null;

            if (!getRegionForControl(sKeyPaintObj, rptSection, isFreeCtrl)) { return _rtn; }

            float w1 = 0;
            float w2 = 0;

            float y = 0;

            cReportSectionLine rtnSecLine = null;

            cReportAspect w_aspect = m_paint.getPaintObject(sKeyPaintObj).getAspect(sKeyPaintObj).Aspect;
            if (isFreeCtrl) {
                y = w_aspect.getTop() + w_aspect.getOffset();
            } 
            else {
                y = w_aspect.getTop() + w_aspect.getHeight() / 2 + w_aspect.getOffset();
            }

            for (int _i = 0; _i < rptSection.getSectionLines().size(); _i++) {
                RptSecLine = RptSection.SectionLines.getItem(_i);
                cReportAspect w_aspect = rptSecLine.getAspect();
                w1 = w_aspect.getTop();
                w2 = w_aspect.getTop() + w_aspect.getHeight();
                if (isFreeCtrl) {
                    if (w1 <= y) {
                        rtnSecLine = rptSecLine;
                    }
                } 
                else {
                    if (w1 <= y && w2 >= y) { return false; }
                }
            }

            if (rtnSecLine != null) {
                rptSecLine = rtnSecLine;
                return true;
            }
            else {
                return false;
            }
        }

        private bool getRegionForControl(String sKeyPaintObj, 
                                            out cReportSection rptSection, 
                                            bool isFreeCtrl) 
        { 
            float x = 0;
            float y = 0;

            cReportAspect w_aspect = m_paint.getPaintObject(sKeyPaintObj).getAspect(sKeyPaintObj).Aspect;
                
            // Headers
            //
            x = w_aspect.getLeft();
            if (isFreeCtrl) {
                y = w_aspect.getTop();
            } 
            else {
                y = w_aspect.getTop() + w_aspect.getHeight() / 2;
            }

            if (getRegionForControlAux(m_report.getHeaders(), x, y, rptSection, isFreeCtrl)) {
                w_aspect.setOffset(0);
                return true;
            }

            // Groups Headers
            //
            if (getRegionForControlAux(m_report.getGroupsHeaders(), x, y, rptSection, isFreeCtrl)) {
                w_aspect.setOffset(0);
                return true;
            }

            // Details
            //
            if (getRegionForControlAux(m_report.getDetails(), x, y, rptSection, isFreeCtrl)) {
                w_aspect.setOffset(0);
                return true;
            }

            // Groups Footers
            //
            if (getRegionForControlAux(m_report.getGroupsFooters(), x, y, rptSection, isFreeCtrl)) {
                w_aspect.setOffset(0);
                return true;
            }

            y = y + m_offSet;

            // Footers
            //
            if (getRegionForControlAux(m_report.getFooters(), x, y, rptSection, isFreeCtrl)) {
                w_aspect.setOffset(m_offSet);
                return true;
            }

            return false;
        }

        private bool getRegionForControlAux(object rptSections, 
                                            float x, 
                                            float y, 
                                            out cReportSection rptSection, 
                                            bool isFreeCtrl) 
        {
            float y1 = 0;
            float y2 = 0;
            cReportSection rtnSec = null;

            for (int _i = 0; _i < rptSections.size(); _i++) {
                RptSection = RptSections.getItem(_i);

                cReportAspect w_aspect = rptSection.getAspect();
                y1 = w_aspect.getTop();
                y2 = w_aspect.getTop() + w_aspect.getHeight();

                if (isFreeCtrl) {
                    if (y1 <= y) {
                        rtnSec = rptSection;
                    }
                } 
                else {
                    if (y1 <= y && y2 >= y) { return true; }
                }
            }

            if (rtnSec != null) {
                rptSection = rtnSec;
                return true;
            }
            else {
                return false;
            }
        }

        private void pChangeTopSection(cReportSection rptSec, 
                                        float offSetTopSection, 
                                        bool bChangeTop, 
                                        bool bZeroOffset) 
        { 
            cReportSectionLine rptSecLine = null;
            cReportControl rptCtrl = null;
            int newTopCtrl = 0;
            int offSet = 0;
            int bottom = 0;

            cReportPaintObject paintSec = null;

            int secTop = 0;
            int secLnHeigt = 0;

            float offSecLn = 0;

            cReportAspect w_aspect = rptSec.getAspect();
            w_aspect.setTop(w_aspect.getTop() + offSetTopSection);

            offSet = rptSec.getSectionLines(1).Aspect.Top - w_aspect.getTop();

            secTop = w_aspect.getTop();

            for (int _i = 0; _i < rptSec.getSectionLines().size(); _i++) {
                RptSecLine = RptSec.SectionLines.getItem(_i);

                cReportAspect w_aspect = rptSecLine.getAspect();

                // footers grow to top
                //
                if (rptSec.getTypeSection() == csRptTypeSection.CSRPTTPMAINSECTIONFOOTER 
                    || rptSec.getTypeSection() == csRptTypeSection.CSRPTTPSCFOOTER) {

                    if (bChangeTop) {

                        if (bZeroOffset) {
                            offSet = 0;
                        }

                    } 
                    else {

                        if (rptSecLine.getrealIndex() >= m_indexSecLnMoved && m_indexSecLnMoved > 0) {

                            bChangeTop = true;
                        }

                    }

                    // every other section grow to bottom
                    //
                } 
                else {
                    offSecLn = (secTop + secLnHeigt) - w_aspect.getTop();

                    if (offSetTopSection) {
                        offSecLn = 0;
                    }
                }

                w_aspect.setTop(secTop + secLnHeigt);
                secLnHeigt = secLnHeigt + w_aspect.getHeight();

                if (rptSecLine.getKeyPaint() != "") {
                    paintSec = m_paint.getPaintSections(rptSecLine.getKeyPaint());
                    paintSec.getAspect().setTop(w_aspect.getTop() + w_aspect.getHeight() - cGlobals.C_HEIGHT_BAR_SECTION);
                } 
                else {
                    paintSec = m_paint.getPaintSections(rptSec.getKeyPaint());
                }
                if (paintSec != null) {
                    paintSec.setHeightSecLine(w_aspect.getHeight());
                }

                for (int _j = 0; _j < rptSecLine.getControls().size(); _j++) {
                    RptCtrl = RptSecLine.Controls.getItem(_j);
                    cReportAspect w_aspect = rptCtrl.getLabel().getAspect();

                    if (rptCtrl.getIsFreeCtrl()) {
                        newTopCtrl = (w_aspect.getTop() - offSet) + offSecLn;
                    } 
                    else {
                        newTopCtrl = (w_aspect.getTop() + w_aspect.getHeight() - offSet) + offSecLn;
                    }

                    cReportAspect w_aspect = getAspect();
                    bottom = w_aspect.getTop() + w_aspect.getHeight();

                    if (newTopCtrl > bottom) {
                        newTopCtrl = bottom - w_aspect.getHeight();
                    } 
                    else {
                        newTopCtrl = (w_aspect.getTop() - offSet) + offSecLn;
                    }

                    cReportAspect w_aspect = getAspect();
                    if (newTopCtrl < w_aspect.getTop()) { newTopCtrl = w_aspect.getTop(); }

                    w_aspect.setTop(newTopCtrl);
                    if (!m_paint.getPaintObject(rptCtrl.getKeyPaint()) == null) {
                        m_paint.getPaintObject(rptCtrl.getKeyPaint()).getAspect().setTop(w_aspect.getTop());
                    }
                }
            }

            // when a group is added the first to get here is the header
            // and the footer has not have a section yet
            //
            if (rptSec.getKeyPaint() == "") { return; }

            cReportAspect w_aspect = rptSec.getAspect();
            
            // we only draw the bottom line of the sections
            //
            paintSec = m_paint.getPaintSections(rptSec.getKeyPaint());
            if (paintSec != null) {
                paintSec.getAspect().setTop(w_aspect.getTop() 
                                            + w_aspect.getHeight() 
                                            - cGlobals.C_HEIGHT_BAR_SECTION);
                paintSec.setHeightSec(w_aspect.getHeight());
            }
        }

        private void moveSection(CSReportPaint.cReportPaintObject paintObj, 
                                    float x, 
                                    float y, 
                                    float minBottom, 
                                    float maxBottom, 
                                    cReportSection secToMove, 
                                    bool isNew) 
        { 
            float oldHeight = 0;
            int i = 0;

            m_dataHasChanged = true;

            cReportAspect w_aspect = paintObj.getAspect();

            // if Y is contained by the premited range everything is ok
            //
            if (y >= minBottom && y <= maxBottom) {
                w_aspect.setTop(y - m_offY);

                // because the top has been set to real dimensions
                // of the screen we must move to the offset
                // of his section
                //
                w_aspect.setTop(w_aspect.getTop() + w_aspect.getOffset());
            } 
            else {
                // if we have moved to top
                //
                if (y < minBottom) {
                    w_aspect.setTop(minBottom);

                    // because the top has been set to real dimensions
                    // of the screen we must move to the offset
                    // of his section
                    //
                    w_aspect.setTop(w_aspect.getTop() + w_aspect.getOffset());
                } 
                else {
                    w_aspect.setTop(maxBottom);
                }
            }

            m_paint.alingToGrid(paintObj.getKey());

            if (isNew) {
                oldHeight = 0;
            } 
            else {
                oldHeight = secToMove.getAspect().getHeight();
            }

            // for the detail section and every other section which is over the detail
            // we only change the height, for all sections bellow the detail we need to
            // change the height and top becasuse wen we strech a section it needs to move
            // to the bottom of the report
            //
            secToMove.getAspect().setHeight(w_aspect.getTop() 
                                            + cGlobals.C_HEIGHT_BAR_SECTION 
                                            - secToMove.getAspect().getTop());

            // every section bellow this section needs to update its top
            //
            float offsetTop = 0;

            cReportAspect w_aspect = secToMove.getAspect();

            offsetTop = oldHeight - (w_aspect.getHeight() + m_newSecLineOffSet);

            switch (secToMove.getTypeSection()) {

                    // if the section is a footer we move to bottom
                    // (Ojo footer sections, no group footers)
                    //
                case  csRptTypeSection.CSRPTTPSCFOOTER:
                case csRptTypeSection.CSRPTTPMAINSECTIONFOOTER:

                    w_aspect.setTop(w_aspect.getTop() + offsetTop);

                    // OJO: this has to be after we have changed the top of the section
                    //      to allow the paint object to reflect the change
                    //
                    // we move the controls of this section
                    //
                    pChangeHeightSection(secToMove, oldHeight);

                    // move the section
                    //
                    pChangeBottomSections(secToMove, offsetTop);

                    // for headers, group headers, group footers and the detail section we move to top
                    //
                    break;
                case  Else:

                    // move all controls in this section
                    //
                    pChangeHeightSection(secToMove, oldHeight);

                    offsetTop = offsetTop * -1;

                    pChangeTopSections(secToMove, offsetTop);
                    break;
            }

            // finaly we need to update the offset of every section,
            // apply it to every object paint in m_Paint
            //
            float pageHeight = 0;
            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            pGetOffSet(mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                        w_paperInfo.getPaperSize(), 
                                                        w_paperInfo.getOrientation()).height, 
                                                        pageHeight);
            pRefreshOffSetInPaintObjs();
            m_paint.setGridHeight(pageHeight);
        }

        private void pChangeBottomSections(cReportSection secToMove, float offsetTop) { 

            cReportSection sec = null;
            bool bChangeTop = null;
            int i = 0;

            if (secToMove.getTypeSection() == csRptTypeSection.CSRPTTPSCFOOTER 
                || secToMove.getTypeSection() == csRptTypeSection.CSRPTTPMAINSECTIONFOOTER 
                || bChangeTop) {

                for (i = m_report.getFooters().count(); i <= 1; i--) {
                    sec = m_report.getFooters(i);

                    if (bChangeTop) {
                        pChangeTopSection(sec, offsetTop, bChangeTop, false);
                    }

                    if (sec == secToMove) {
                        bChangeTop = true;
                    }
                }
            }
        }

        private void pChangeTopSections(cReportSection secToMove, float offsetTop) { 

            cReportSection sec = null;
            bool bChangeTop = null;
            int i = 0;

            if (secToMove.getTypeSection() == csRptTypeSection.CSRPTTPSCHEADER 
                || secToMove.getTypeSection() == csRptTypeSection.CSRPTTPMAINSECTIONHEADER) {

                for (int _i = 0; _i < m_report.getHeaders().size(); _i++) {
                    Sec = m_Report.Headers.getItem(_i);
                    if (bChangeTop) {
                        pChangeTopSection(sec, offsetTop, bChangeTop, false);
                    }

                    if (sec == secToMove) {
                        bChangeTop = true;
                    }
                }
            }

            if (secToMove.getTypeSection() == csRptTypeSection.CSRPTTPGROUPHEADER || bChangeTop) {

                for (int _i = 0; _i < m_report.getGroupsHeaders().size(); _i++) {
                    Sec = m_Report.GroupsHeaders.getItem(_i);
                    if (bChangeTop) {
                        pChangeTopSection(sec, offsetTop, bChangeTop, false);
                    }

                    if (sec == secToMove) {
                        bChangeTop = true;
                    }
                }
            }

            if (secToMove.getTypeSection() == csRptTypeSection.CSRPTTPMAINSECTIONDETAIL 
                || secToMove.getTypeSection() == csRptTypeSection.CSRPTTPSCDETAIL || bChangeTop) {

                for (int _i = 0; _i < m_report.getDetails().size(); _i++) {
                    Sec = m_Report.Details.getItem(_i);
                    if (bChangeTop) {
                        pChangeTopSection(sec, offsetTop, bChangeTop, false);
                    }

                    if (sec == secToMove) {
                        bChangeTop = true;
                    }
                }
            }

            if (secToMove.getTypeSection() == csRptTypeSection.CSRPTTPGROUPFOOTER || bChangeTop) {

                for (int _i = 0; _i < m_report.getGroupsFooters().size(); _i++) {
                    Sec = m_Report.GroupsFooters.getItem(_i);
                    if (bChangeTop) {
                        pChangeTopSection(sec, offsetTop, bChangeTop, false);
                    }

                    if (sec == secToMove) {
                        bChangeTop = true;
                    }
                }
            }
        }

        private void pChangeHeightSection(cReportSection sec, float oldSecHeight) {
            int i = 0;
            float heightLines = 0;

            // Update section line
            //
            for (i = 1; i <= sec.getSectionLines().count() - 1; i++) {
                cReportAspect w_aspect = sec.getSectionLines().item(i).getAspect(i).Aspect;
                heightLines = heightLines + w_aspect.getHeight();
            }

            // for the last section line the height is the rest
            //
            cReportSectionLines w_sectionLines = sec.getSectionLines();
            cReportAspect w_aspect = w_sectionLines.item(w_sectionLines.count()).getAspect();
            w_aspect.setHeight(sec.getAspect().getHeight() - heightLines);

            pChangeTopSection(sec, 0, false, true);
        }

        private void reLoadReport() {

            cReportPaintObject paintSec = null;

            m_paint = null;

            m_keyMoving = "";
            m_keySizing = "";
            m_keyObj = "";
            m_keyFocus = "";
            m_moveType = CSRptEditroMoveType.csRptEdMovTNone;

            m_paint = new CSReportPaint.cReportPaint();

            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            m_paint.setGridHeight(
                    pSetSizePics(mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                                w_paperInfo.getPaperSize(), 
                                                                w_paperInfo.getOrientation()).height));

            m_paint.initGrid(m_picReport, m_typeGrid);

            if (!m_report.getName() == "") {
                Me.Caption = m_report.getPath() + m_report.getName();
            }

            cReportSection sec = null;

            for (int _i = 0; _i < m_report.getHeaders().size(); _i++) {
                Sec = m_Report.Headers.getItem(_i);
                sec.setKeyPaint(paintSection(sec.getAspect(), 
                                                sec.getKey(), 
                                                sec.getTypeSection(), 
                                                sec.getName(), 
                                                false));
                paintSec = m_paint.getPaintSections(sec.getKeyPaint());
                paintSec.setHeightSec(sec.getAspect().getHeight());
                pAddPaintSetcionForSecLn(sec, csESectionLineTypes.C_KEY_SECLN_HEADER);
            }

            for (int _i = 0; _i < m_report.getGroupsHeaders().size(); _i++) {
                Sec = m_Report.GroupsHeaders.getItem(_i);
                sec.setKeyPaint(paintSection(sec.getAspect(), 
                                                sec.getKey(), 
                                                sec.getTypeSection(), 
                                                sec.getName(), 
                                                false));
                paintSec = m_paint.getPaintSections(sec.getKeyPaint());
                paintSec.setHeightSec(sec.getAspect().getHeight());
                pAddPaintSetcionForSecLn(sec, csESectionLineTypes.C_KEY_SECLN_GROUPH);
            }

            for (int _i = 0; _i < m_report.getDetails().size(); _i++) {
                Sec = m_Report.Details.getItem(_i);
                sec.setKeyPaint(paintSection(sec.getAspect(), 
                                                sec.getKey(), 
                                                sec.getTypeSection(), 
                                                sec.getName(), 
                                                false));
                paintSec = m_paint.getPaintSections(sec.getKeyPaint());
                paintSec.setHeightSec(sec.getAspect().getHeight());
                pAddPaintSetcionForSecLn(sec, csESectionLineTypes.C_KEY_SECLN_DETAIL);
            }

            for (int _i = 0; _i < m_report.getGroupsFooters().size(); _i++) {
                Sec = m_Report.GroupsFooters.getItem(_i);
                sec.setKeyPaint(paintSection(sec.getAspect(), 
                                                sec.getKey(), 
                                                sec.getTypeSection(), 
                                                sec.getName(), 
                                                false));
                paintSec = m_paint.getPaintSections(sec.getKeyPaint());
                paintSec.setHeightSec(sec.getAspect().getHeight());
                pAddPaintSetcionForSecLn(sec, csESectionLineTypes.C_KEY_SECLN_GROUPF);
            }

            for (int _i = 0; _i < m_report.getFooters().size(); _i++) {
                Sec = m_Report.Footers.getItem(_i);
                sec.setKeyPaint(paintSection(sec.getAspect(), 
                                                sec.getKey(), 
                                                sec.getTypeSection(), 
                                                sec.getName(), 
                                                false));
                paintSec = m_paint.getPaintSections(sec.getKeyPaint());
                paintSec.setHeightSec(sec.getAspect().getHeight());
                pAddPaintSetcionForSecLn(sec, csESectionLineTypes.C_KEY_SECLN_FOOTER);
            }

            CSReportPaint.cReportPaintObject paintObj = null;
            cReportControl rptCtrl = null;
            cReportAspect ctrlAspect = null;

            CSReportPaint.csRptPaintObjType paintType = null;

            for (int _i = 0; _i < m_report.getControls().size(); _i++) {
                RptCtrl = m_Report.Controls.getItem(_i);
                refreshNextNameCtrl(rptCtrl.getName());
                ctrlAspect = rptCtrl.getLabel().getAspect();

                if (rptCtrl.getControlType() == csRptControlType.CSRPTCTIMAGE 
                    || rptCtrl.getControlType() == csRptControlType.CSRPTCTCHART) {
                    paintType = CSReportPaint.csRptPaintObjType.CSRPTPAINTOBJIMAGE;
                } 
                else {
                    paintType = CSReportPaint.csRptPaintObjType.CSRPTPAINTOBJBOX;
                }

                paintObj = m_paint.getNewObject(paintType);

                // for old reports
                //
                ctrlAspect.setTransparent(ctrlAspect.getBackColor() == vbWhite);

                paintObj.setHImage(rptCtrl.getImage().getHImage());

                cReportAspect w_aspect = paintObj.getAspect();
                w_aspect.setLeft(ctrlAspect.getLeft());
                w_aspect.setTop(ctrlAspect.getTop());
                w_aspect.setWidth(ctrlAspect.getWidth());
                w_aspect.setHeight(ctrlAspect.getHeight());
                w_aspect.setBackColor(ctrlAspect.getBackColor());
                w_aspect.setTransparent(ctrlAspect.getTransparent());
                w_aspect.setAlign(ctrlAspect.getAlign());
                w_aspect.setWordWrap(ctrlAspect.getWordWrap());

                if (ctrlAspect.getBorderType() == CSRPTBSNONE) {
                    w_aspect.setBorderColor(vbBlack);
                    w_aspect.setBorderWidth(1);
                    w_aspect.setBorderRounded(false);
                    w_aspect.setBorderType(CSRPTBSFIXED);
                } 
                else {
                    w_aspect.setBorderType(ctrlAspect.getBorderType());
                    w_aspect.setBorderColor(ctrlAspect.getBorderColor());
                    w_aspect.setBorderColor3d(ctrlAspect.getBorderColor3d());
                    w_aspect.setBorderColor3dShadow(ctrlAspect.getBorderColor3dShadow());
                    w_aspect.setBorderRounded(ctrlAspect.getBorderRounded());
                    w_aspect.setBorderWidth(ctrlAspect.getBorderWidth());
                }

                switch (rptCtrl.getSectionLine().getTypeSection()) {
                    case  csRptTypeSection.CSRPTTPSCFOOTER:
                    case  csRptTypeSection.CSRPTTPMAINSECTIONFOOTER:
                        w_aspect.setOffset(m_offSet);
                        break;
                }

                cReportFont w_font = w_aspect.getFont();
                w_font.setName(ctrlAspect.getFont().getName());
                w_font.setForeColor(ctrlAspect.getFont().getForeColor());
                w_font.setSize(ctrlAspect.getFont().getSize());
                w_font.setBold(ctrlAspect.getFont().getBold());
                w_font.setItalic(ctrlAspect.getFont().getItalic());
                w_font.setUnderLine(ctrlAspect.getFont().getUnderline());
                w_font.setStrike(ctrlAspect.getFont().getStrike());

                paintObj.setText(rptCtrl.getLabel().getText());
                paintObj.setRptType(CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPECONTROL);
                paintObj.setTag(rptCtrl.getKey());
                rptCtrl.setKeyPaint(paintObj.getKey());
            }

            m_dataHasChanged = false;

            m_paint.createPicture(m_picReport);

            m_picRule.Refresh;
        }

        private void pAddPaintSetcionForSecLn(
            cReportSection sec, 
            csESectionLineTypes typeSecLn) 
        { 
            int i = 0;
            cReportPaintObject paintSec = null;

            if (sec.getSectionLines().count() > 1) {

                for (i = 1; i <= sec.getSectionLines().count() - 1; i++) {
                    cReportSectionLines w_sectionLines = sec.getSectionLines(i);
                    w_sectionLines.KeyPaint = paintSection(w_sectionLines.Aspect, 
                                                            w_sectionLines.Key, 
                                                            sec.getTypeSection(), 
                                                            C_SECTIONLINE + i.ToString(), 
                                                            true);

                    // we set the height of every section line
                    //
                    paintSec = m_paint.getPaintSections().item(w_sectionLines.KeyPaint);
                    paintSec.setHeightSecLine(w_sectionLines.Aspect.Height);
                    paintSec.setRptType(typeSecLn);
                    paintSec.setRptKeySec(sec.getKey());
                }

                // if there is more than one section we use
                // textLine to show the name of the last line
                //
                CSReportPaint.cReportPaintObject po = m_paint.getPaintSections().item(sec.getKeyPaint());
                po.TextLine = C_SECTIONLINE + sec.getSectionLines().count().ToString();
            }

            // we set the height of the last section line
            //
            paintSec = m_paint.getPaintSections().item(sec.getKeyPaint());

            cReportSectionLines w_sectionLines = sec.getSectionLines();
            paintSec.setHeightSecLine(w_sectionLines.item(w_sectionLines.count()).getAspect().getHeight());
        }

        private void refreshNextNameCtrl(String nameCtrl) {
            int x = 0;
            if (nameCtrl.Substring(1, C_Control_Name.Length).ToUpper() == C_Control_Name.ToUpper()) {
                x = Val(nameCtrl.Substring(C_Control_Name.Length + 1));
                if (x > m_nextNameCtrl) {
                    m_nextNameCtrl = x + 1;
                }
            }
        }

        private void moveControl(String sKeyPaintObj) {
            cReportSectionLine rptSecLine = null;
            cReportControl rptCtrl = null;
            cReportAspect rptSecLineAspect = null;
            CSReportPaint.cReportAspect objPaintAspect = null;

            m_paint.alingToGrid(sKeyPaintObj);

            rptCtrl = m_report.getControls().item(m_paint.getPaintObject(sKeyPaintObj).getTag());

            objPaintAspect = m_paint.getPaintObject(sKeyPaintObj).getAspect();

            if (rptCtrl == null) { return; }

            cReportAspect w_aspect = rptCtrl.getLabel().getAspect();
            w_aspect.setTop(objPaintAspect.getTop() + objPaintAspect.getOffset());
            w_aspect.setHeight(objPaintAspect.getHeight());
            w_aspect.setWidth(objPaintAspect.getWidth());
            w_aspect.setLeft(objPaintAspect.getLeft());

            if (getLineRegionForControl(sKeyPaintObj, rptSecLine, rptCtrl.getIsFreeCtrl())) {

                if (!(rptSecLine == rptCtrl.getSectionLine())) {
                    rptCtrl.getSectionLine().getControls().remove(rptCtrl.getKey());
                    rptSecLine.getControls().add(rptCtrl, rptCtrl.getKey());
                }

                // we need to check the control is between the limits of the section
                // in which it is contained
                //
                rptSecLineAspect = rptCtrl.getSectionLine().getAspect();

                cReportAspect w_aspect = rptCtrl.getAspect();

                w_aspect.setTop(objPaintAspect.getTop() + objPaintAspect.getOffset());

                if (!rptCtrl.getIsFreeCtrl()) {
                    if (w_aspect.getTop() + w_aspect.getHeight() 
                        > rptSecLineAspect.getTop() + rptSecLineAspect.getHeight()) {
                        w_aspect.setTop(rptSecLineAspect.getTop() 
                                        + rptSecLineAspect.getHeight() 
                                        - w_aspect.getHeight());
                    }
                }

                if (w_aspect.getTop() < rptSecLineAspect.getTop()) {
                    w_aspect.setTop(rptSecLineAspect.getTop());
                }

                objPaintAspect.setTop(w_aspect.getTop());
            }
        }

        private void showPopMenuSection(bool noDelete, bool showGroups) {
            fMain.popSecDelete.Enabled = !noDelete;
            fMain.popSecPropGroup.Visible = showGroups;
            fMain.PopupMenu(fMain.popSec);
        }

        private void showPopMenuControl(bool clickInCtrl) {
            if (!clickInCtrl) {
                fMain.popObjCopy.Enabled = false;
                fMain.popObjCut.Enabled = false;
                fMain.popObjDelete.Enabled = false;
                fMain.popObjEditText.Enabled = false;
                fMain.popObjSendToBack.Enabled = false;
                fMain.popObjBringToFront.Enabled = false;
                fMain.popObjSendToBack.Enabled = false;
                fMain.popObjProperties.Enabled = false;
            } 
            else {
                fMain.popObjCopy.Enabled = true;
                fMain.popObjCut.Enabled = true;
                fMain.popObjDelete.Enabled = true;
                fMain.popObjEditText.Enabled = true;
                fMain.popObjSendToBack.Enabled = true;
                fMain.popObjBringToFront.Enabled = true;
                fMain.popObjSendToBack.Enabled = true;
                fMain.popObjProperties.Enabled = true;
            }

            bool bPasteEnabled = null;

            if (m_vCopyKeys.Length > 0) {
                bPasteEnabled = true;
            } 
            else if (!(fMain.getReportCopySource() == null)) {
                bPasteEnabled = fMain.getReportCopySource().getVCopyKeysCount() > 0;
            }

            fMain.popObjPaste.Enabled = bPasteEnabled;
            fMain.popObjPasteEx.Enabled = bPasteEnabled;
            fMain.PopupMenu(w___TYPE_NOT_FOUND.popObj);
        }

        private void m_fGroup_UnloadForm() {
            m_fGroup = null;
        }

        private void m_fProperties_UnloadForm() {
            m_fProperties = null;
        }

        private void refreshBody() {
            try {

                m_paint.endMove(m_picReport);

            } catch (Exception ex) {
                MngError(VBA.ex, "ShowConnectsAux", C_MODULE, "");
            }
        }

        private void refreshRule() {
            m_picRule.Refresh;
        }

        public void refreshReport() {

            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            m_paint.setGridHeight(pSetSizePics(
                                    mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                                    w_paperInfo.getPaperSize(), 
                                                                    w_paperInfo.getOrientation()).height));

            sizeControls();

            pValidateSectionAspect();
            reLoadReport();
        }

        public void refreshPostion() {
            scrHorizontal_Change();
            scrVertical_Change();
        }

        public void refreshAll() {
            refreshBody();
            refreshRule();
        }

        private void m_Report_Done() {
            closeProgressDlg();
        }

        private void m_Report_Progress(
            String task, 
            int page, 
            int currRecord, 
            int recordCount, 
            out bool cancel) 
        { 
            Application.DoEvents;

            if (m_cancelPrinting) {
                if (Ask("Confirm you want to cancel the execution of this report?", vbNo)) {
                    cancel = true;
                    closeProgressDlg();
                    return;
                } 
                else {
                    m_cancelPrinting = false;
                }
            }

            if (m_fProgress == null) { return; }

            if (page > 0) { m_fProgress.lbCurrPage.Caption = page; }
            if (task != "") { m_fProgress.lbTask.Caption = task; }
            if (currRecord > 0) { m_fProgress.lbCurrRecord.Caption = currRecord; }
            if (recordCount > 0 && Val(m_fProgress.lbRecordCount.Caption) != recordCount) { 
                m_fProgress.lbRecordCount.Caption = recordCount; 
            }

            double percent = 0;
            if (recordCount > 0 && currRecord > 0) {
                percent = currRecord / recordCount;
                m_fProgress.prgVar.Value = percent * 100;
            }
        }

        private void closeProgressDlg() {
            Unload(m_fProgress);
            m_fProgress = null;
        }

        private void showProgressDlg() {
            m_cancelPrinting = false;
            if (m_fProgress == null) { 
                m_fProgress = new fProgress();
                // TODO: add event for m_Report_Progress
            }
            m_fProgress.Show;
            m_fProgress.ZOrder;
        }

        private void m_fProgress_Cancel() {
            m_cancelPrinting = true;
        }

        private void m_Report_FindFileAccess(
            out bool answer, 
            object commDialog, 
            String file) 
        { 
            String msg = "";
            msg = "The " + file + " could not be found. ¿Do you want to find it?";
            if (!Ask(msg, vbYes)) { return; }

            commDialog = fMain.cmDialog;
            answer = true;
            m_fProgress.ZOrder;
            m_dataHasChanged = true;
        }

        private void txEdit_KeyPress(int keyAscii) {
            if (keyAscii == vbKeyEscape) {
                endEditText(keyAscii == vbKeyEscape);
                keyAscii = 0;
            }
        }

        private object pGetLeftBody() {
            Object _rtn = null;
            if (mPublic.gHideLeftBar) {
                _rtn = C_LEFTBODY;
            } 
            else {
                _rtn = m_picRule.Width + C_LEFTBODY;
            }
            return _rtn;
        }

        private float pSetSizePics(float realPageHeight) {
            float pageHeight = 0;

            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            m_picReport.Width = mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                                w_paperInfo.getPaperSize(), 
                                                                w_paperInfo.getOrientation()).width;
            pGetOffSet(realPageHeight, pageHeight);

            if (pageHeight > realPageHeight) { realPageHeight = pageHeight; }

            m_picReport.Height = realPageHeight;
            m_picRule.Height = realPageHeight + C_TOPBODY * 2 + ScrVertical.Height;

            return pageHeight;
        }

        private void pMoveAll(float x, float y) {
            cReportAspect rptCtrlAspect = null;
            CSReportPaint.cReportPaintObject paintObj = null;

            m_dataHasChanged = true;

            if (m_bNoMove) { return; }

            int i = 0;
            int offsetTop = 0;
            int offsetLeft = 0;
            int firstLeft = 0;
            int firstTop = 0;
            int firstOffSet = 0;

            if (m_vSelectedKeys.Length == 0) { return; }

            paintObj = m_paint.getPaintObject(m_keyMoving);

            cReportAspect w_aspect = paintObj.getAspect();
            firstLeft = w_aspect.getLeft();
            firstTop = w_aspect.getTop();
            firstOffSet = w_aspect.getOffset();

            for (i = m_vSelectedKeys.Length; i <= 1; i--) {

                paintObj = m_paint.getPaintObject(m_vSelectedKeys[i]);

                offsetLeft = pGetOffsetLeftFromControls(firstLeft, 
                                                        paintObj.getAspect().getLeft());

                offsetTop = pGetOffsetTopFromControls(firstTop - firstOffSet, 
                                                        paintObj.getAspect().getTop() 
                                                        - paintObj.getAspect().getOffset());

                cReportAspect w_aspect = paintObj.getAspect();

                if (x != C_NOMOVE) {
                    w_aspect.setLeft(x - m_offX + offsetLeft);
                }

                if (y != C_NOMOVE) {
                    w_aspect.setTop(y - m_offY + offsetTop);
                } 
                else {

                    // we get rid off the offset because the primitive
                    // add it to the top but we don't allow vertical
                    // moves so Y must to remain constant
                    //
                    w_aspect.setTop(w_aspect.getTop() - paintObj.getAspect().getOffset());
                }

                // only controls move in all directions
                // 
                if (paintObj.getRptType() == CSReportPaint.csRptPaintRptType.CSRPTPAINTRPTTYPECONTROL) {
                    rptCtrlAspect = m_report.getControls().item(paintObj.getTag()).getLabel().getAspect();
                    rptCtrlAspect.setLeft(w_aspect.getLeft());
                    rptCtrlAspect.setTop(w_aspect.getTop());
                    rptCtrlAspect.setWidth(w_aspect.getWidth());
                    rptCtrlAspect.setHeight(w_aspect.getHeight());
                }

                moveControl(m_vSelectedKeys[i]);
            }
        }

        private void pMoveHorizontal(float x) {
            m_dataHasChanged = true;
            m_paint.getPaintObject(m_keyMoving).getAspect().setLeft(x - m_offX);
        }

        private void pMoveVertical(float x, float y) {
            String sKeySection = "";
            CSReportPaint.csRptPaintRptType rptType = null;

            int maxBottom = 0;
            int minBottom = 0;

            cReportSection rptSec = null;
            CSReportPaint.cReportPaintObject paintObj = null;
            bool isSecLn = null;

            m_indexSecLnMoved = -1;

            paintObj = m_paint.getPaintObject(m_keyMoving);
            cReportAspect w_aspect = paintObj.getAspect();

            sKeySection = paintObj.getTag();

            // sections can only be move verticaly
            // always is the bottom of the section which is moved
            // every time we move a section the height change
            //
            rptType = paintObj.getRptType();


            switch (rptType) {

                    //---------------------
                    // HEADER
                    //---------------------

                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeMainSectionHeader:
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeSectionHeader:

                    rptSec = pMoveHeader(sKeySection, minBottom, maxBottom, false);

                    //---------------------
                    // GROUP HEADER
                    //---------------------

                    break;
                
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeGroupSectionHeader:

                    rptSec = pMoveGroupHeader(sKeySection, minBottom, maxBottom, false);

                    //---------------------
                    // DETAIL
                    //---------------------

                    break;
                
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeMainSectionDetail:
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeSectionDetail:

                    rptSec = pMoveDetails(sKeySection, minBottom, maxBottom, false);

                    //---------------------
                    // GROUP FOOTER
                    //---------------------

                    break;
                
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeGroupSectionFooter:

                    rptSec = pMoveGroupFooter(sKeySection, minBottom, maxBottom, false);

                    //---------------------
                    // FOOTER
                    //---------------------

                    break;
                
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeMainSectionFooter:
                case  CSReportPaint.csRptPaintRptType.csRptPaintRptTypeSectionFooter:

                    rptSec = pMoveFooter(sKeySection, minBottom, maxBottom, false);

                    //---------------------
                    // Section Lines
                    //---------------------
                    break;

                case  csESectionLineTypes.C_KEY_SECLN_HEADER:
                    sKeySection = paintObj.getRptKeySec();
                    rptSec = pMoveHeader(sKeySection, minBottom, maxBottom, true);
                    isSecLn = true;
                    break;

                case  csESectionLineTypes.C_KEY_SECLN_GROUPH:
                    sKeySection = paintObj.getRptKeySec();
                    rptSec = pMoveGroupHeader(sKeySection, minBottom, maxBottom, true);
                    isSecLn = true;
                    break;

                case  csESectionLineTypes.C_KEY_SECLN_DETAIL:
                    sKeySection = paintObj.getRptKeySec();
                    rptSec = pMoveDetails(sKeySection, minBottom, maxBottom, true);
                    isSecLn = true;
                    break;

                case  csESectionLineTypes.C_KEY_SECLN_GROUPF:
                    sKeySection = paintObj.getRptKeySec();
                    rptSec = pMoveGroupFooter(sKeySection, minBottom, maxBottom, true);
                    isSecLn = true;
                    break;

                case  csESectionLineTypes.C_KEY_SECLN_FOOTER:
                    sKeySection = paintObj.getRptKeySec();
                    rptSec = pMoveFooter(sKeySection, minBottom, maxBottom, true);
                    isSecLn = true;
                    m_indexSecLnMoved = rptSec.getSectionLines(paintObj.getTag()).realIndex;
                    break;
            }

            if (isSecLn) {
                minBottom = pGetMinBottomForSecLn(rptSec, paintObj.getTag(), minBottom);
                pChangeSecLnHeight(paintObj, 
                                    y, 
                                    minBottom, 
                                    maxBottom, 
                                    rptSec.getSectionLines(paintObj.getTag()));

                y = rptSec.getAspect().getTop() 
                    - paintObj.getAspect().getOffset() 
                    + pGetSecHeigthFromSecLines(rptSec) 
                    - cGlobals.C_HEIGHT_BAR_SECTION;

                m_offY = 0;
                paintObj = m_paint.getPaintSections(rptSec.getKeyPaint());
            }

            moveSection(paintObj, x, y, minBottom, maxBottom, rptSec, false);
        }

        private int pGetSecHeigthFromSecLines(cReportSection sec) {
            int rtn = 0;
            cReportSectionLine secLn = null;

            for (int _i = 0; _i < sec.getSectionLines().size(); _i++) {
                SecLn = Sec.SectionLines.getItem(_i);
                rtn = rtn + secLn.getAspect().getHeight();
            }

            return rtn;
        }

        private int pGetMinBottomForSecLn(
            cReportSection sec, 
            String secLnKey, 
            int minBottom) 
        {
            cReportSectionLine secLn = null;

            for (int _i = 0; _i < sec.getSectionLines().size(); _i++) {
                SecLn = Sec.SectionLines.getItem(_i);
                if (secLn.getKey() == secLnKey) { break; }
                minBottom = minBottom + secLn.getAspect().getHeight();
            }
            return minBottom;
        }

        private void pChangeSecLnHeight(
            CSReportPaint.cReportPaintObject paintObj, 
            float y, 
            int minBottom, 
            int maxBottom, 
            cReportSectionLine secLn) 
        { 
            cReportAspect w_aspect = paintObj.getAspect();

            // if Y is contained between the range allowed everything is ok
            //
            if (y >= minBottom && y <= maxBottom) {
                w_aspect.setTop(y - m_offY);
            } 
            else {
                // if it have been moved upward
                //
                if (y < minBottom) {
                    w_aspect.setTop(minBottom);

                } 
                // if it have been moved downward
                //
                else {
                    w_aspect.setTop(maxBottom);
                }
            }

            // because the top has been setted to the real dimensions
            // of the screen now we need to move it the offset
            // of its section
            //
            w_aspect.setTop(w_aspect.getTop() + w_aspect.getOffset());

            m_paint.alingToGrid(paintObj.getKey());

            // the section line height has been changed
            //
            secLn.getAspect().setHeight(w_aspect.getTop() 
                                        + cGlobals.C_HEIGHT_BAR_SECTION 
                                        - secLn.getAspect().getTop());
        }

        private void pSizingControl(float x, float y) {
            int i = 0;
            int height = 0;
            int width = 0;
            int left = 0;
            int top = 0;

            if (m_vSelectedKeys.Length == 0) { return; }

            m_dataHasChanged = true;

            // first we need to modify the control which has its size changed
            //
            cReportPaintObject w_getPaintObject = m_paint.getPaintObject(m_keySizing);
            cReportAspect w_aspect = w_getPaintObject.getAspect();

            // orginal size to know how much it has changed
            //
            height = w_aspect.getHeight();
            width = w_aspect.getWidth();
            left = w_aspect.getLeft();
            top = w_aspect.getTop();

            switch (m_moveType) {
                case  CSRptEditroMoveType.csRptEdMovDown:
                    w_aspect.setHeight(y - (w_aspect.getTop() - w_aspect.getOffset()));
                    break;
                case  CSRptEditroMoveType.csRptEdMovLeft:
                    w_aspect.setWidth(w_aspect.getWidth() + w_aspect.getLeft() - x);
                    w_aspect.setLeft(x);
                    break;
                case  CSRptEditroMoveType.csRptEdMovRight:
                    w_aspect.setWidth(x - w_aspect.getLeft());
                    break;
                case  CSRptEditroMoveType.csRptEdMovUp:
                    w_aspect.setHeight(w_aspect.getHeight() + (w_aspect.getTop() - w_aspect.getOffset()) - y);
                    w_aspect.setTop(y + w_aspect.getOffset());
                    break;
                case  CSRptEditroMoveType.csRptEdMovLeftDown:
                    w_aspect.setHeight(y - (w_aspect.getTop() - w_aspect.getOffset()));
                    w_aspect.setWidth(w_aspect.getWidth() + w_aspect.getLeft() - x);
                    w_aspect.setLeft(x);
                    break;
                case  CSRptEditroMoveType.csRptEdMovLeftUp:
                    w_aspect.setHeight(w_aspect.getHeight() + (w_aspect.getTop() - w_aspect.getOffset()) - y);
                    w_aspect.setTop(y + w_aspect.getOffset());
                    w_aspect.setWidth(w_aspect.getWidth() + w_aspect.getLeft() - x);
                    w_aspect.setLeft(x);
                    break;
                case  CSRptEditroMoveType.csRptEdMovRightDown:
                    w_aspect.setWidth(x - w_aspect.getLeft());
                    w_aspect.setHeight(y - (w_aspect.getTop() - w_aspect.getOffset()));
                    break;
                case  CSRptEditroMoveType.csRptEdMovRightUp:
                    w_aspect.setHeight(w_aspect.getHeight() + (w_aspect.getTop() - w_aspect.getOffset()) - y);
                    w_aspect.setTop(y + w_aspect.getOffset());
                    w_aspect.setWidth(x - w_aspect.getLeft());
                    break;
            }

            top = w_aspect.getTop() - top;
            left = w_aspect.getLeft() - left;
            width = w_aspect.getWidth() - width;
            height = w_aspect.getHeight() - height;

            pMoveControl(w_getPaintObject.getAspect(), true);

            for (i = 1; i <= m_vSelectedKeys.Length; i++) {

                if (m_keySizing != m_vSelectedKeys[i]) {

                    cReportPaintObject w_getPaintObject = m_paint.getPaintObject(m_vSelectedKeys[i]);
                    cReportAspect w_aspect = w_getPaintObject.getAspect();

                    w_aspect.setHeight(w_aspect.getHeight() + height);
                    w_aspect.setTop(w_aspect.getTop() + top);
                    w_aspect.setWidth(w_aspect.getWidth() + width);
                    w_aspect.setLeft(w_aspect.getLeft() + left);

                    pMoveControl(w_getPaintObject.getAspect(), false);
                }
            }
        }

        private void pMoveControl(CSReportPaint.cReportAspect aspect, bool bSizing) { 
            const int C_MIN_WIDTH = 10;
            const int C_MIN_HEIGHT = 10;

            cReportAspect rptCtrlAspect = null;

            if (m_paint.getPaintObject(m_keySizing).getRptType() == CSReportPaint.CSRPTPAINTRPTTYPECONTROL) {
                rptCtrlAspect = m_report.getControls().item(m_paint.getPaintObject(m_keySizing).getTag()).getLabel().getAspect();
                rptCtrlAspect.setLeft(aspect.getLeft());
                if (!bSizing) {
                    rptCtrlAspect.setTop(aspect.getTop() + aspect.getOffset());
                } 
                else {
                    rptCtrlAspect.setTop(aspect.getTop());
                }
                rptCtrlAspect.setWidth(aspect.getWidth());
                rptCtrlAspect.setHeight(aspect.getHeight());
            }

            switch (m_moveType) {
                case  CSRptEditroMoveType.csRptEdMovDown:
                    m_paint.alingObjBottomToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovLeft:
                    m_paint.alingObjLeftToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovRight:
                    m_paint.alingObjRightToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovUp:
                    m_paint.alingObjTopToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovLeftDown:
                    m_paint.alingObjLeftBottomToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovLeftUp:
                    m_paint.alingObjLeftTopToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovRightDown:
                    m_paint.alingObjRightBottomToGrid(m_keySizing);
                    break;
                case  CSRptEditroMoveType.csRptEdMovRightUp:
                    m_paint.alingObjRightTopToGrid(m_keySizing);
                    break;
            }

            // Validations

            // Width can't be lower than C_MIN_WIDTH
            //
            if (aspect.getWidth() < C_MIN_WIDTH) { aspect.setWidth(C_MIN_WIDTH); }
            // Height can't be lower than C_MIN_HEIGHT
            //
            if (aspect.getHeight() < C_MIN_HEIGHT) { aspect.setHeight(C_MIN_HEIGHT); }
        }

        private cReportSection pMoveHeader(
            String sKeySection, 
            int minBottom, 
            int maxBottom, 
            bool isForSectionLine) 
        {
            int index = 0;
            cReportSection rptSec = null;

            rptSec = m_report.getHeaders().item(sKeySection);

            index = rptSec.getrealIndex();

            //-----------
            // MinBottom
            //-----------
            if (index == 1) {
                minBottom = C_MIN_HEIGHT_SECTION;
            } 
            else {
                // bottom of previous header + C_Min_Height_Section
                cReportAspect w_aspect = m_report.getHeaders().item(index - 1).getAspect(index - 1).Aspect;
                minBottom = w_aspect.getTop() + w_aspect.getHeight() + C_MIN_HEIGHT_SECTION;
            }

            if (!isForSectionLine) {
                minBottom = pGetMinBottomWithSecLn(rptSec.getSectionLines(), minBottom);
            }

            maxBottom = m_picReport.Height;

            return rptSec;
        }

        private cReportSection pMoveGroupHeader(
            String sKeySection, 
            int minBottom, 
            int maxBottom, 
            bool isForSectionLine) 
        {
            int index = 0;
            cReportSection rptSec = null;

            rptSec = m_report.getGroupsHeaders().item(sKeySection);

            index = rptSec.getrealIndex();

            //-----------
            // MinBottom
            //-----------
            if (index == 1) {
                // bottom of previous header + C_Min_Height_Section
                cReportSections w_headers = m_report.getHeaders();
                cReportAspect w_aspect = w_headers.item(w_headers.count()).getAspect();
                minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
            } 
            else {
                // bottom of previous group header + C_Min_Height_Section
                cReportAspect w_aspect = m_report.getGroupsHeaders().item(index - 1).getAspect(index - 1).Aspect;
                minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
            }

            if (!isForSectionLine) {
                minBottom = pGetMinBottomWithSecLn(rptSec.getSectionLines(), minBottom);
            }

            maxBottom = m_picReport.Height;

            return rptSec;
        }

        private cReportSection pMoveDetails(
            String sKeySection, 
            int minBottom, 
            int maxBottom, 
            bool isForSectionLine) 
        { 
            int index = 0;
            cReportSection rptSec = null;

            rptSec = m_report.getDetails().item(sKeySection);

            index = rptSec.getrealIndex();

            //-----------
            // MinBottom
            //-----------

            if (index == 1) {
                // if there are groups
                //
                if (m_report.getGroupsHeaders().count() > 0) {
                    // top of the last group header + C_Min_Height_Section
                    cIReportGroupSections w_groupsHeaders = m_report.getGroupsHeaders();
                    cReportAspect w_aspect = w_groupsHeaders.item(w_groupsHeaders.count()).getAspect();
                    minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
                } 
                else {
                    // top of the last header + C_Min_Height_Section
                    cReportSections w_headers = m_report.getHeaders();
                    cReportAspect w_aspect = w_headers.item(w_headers.count()).getAspect();
                    minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
                }
            } 
            else {
                // top of the previous detail + C_Min_Height_Section
                //
                cReportAspect w_aspect = m_report.getDetails().item(index - 1).getAspect(index - 1).Aspect;
                minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
            }

            if (!isForSectionLine) {
                minBottom = pGetMinBottomWithSecLn(rptSec.getSectionLines(), minBottom);
            }

            maxBottom = m_picReport.Height;

            return rptSec;
        }

        private cReportSection pMoveGroupFooter(
            String sKeySection, 
            int minBottom, 
            int maxBottom, 
            bool isForSectionLine) 
        {
            int index = 0;
            cReportSection rptSec = null;

            rptSec = m_report.getGroupsFooters().item(sKeySection);

            index = rptSec.getrealIndex();

            //-----------
            // MinBottom
            //-----------
            if (index == 1) {
                // bottom of the last detail + C_Min_Height_Section
                //
                cReportSections w_details = m_report.getDetails();
                cReportAspect w_aspect = w_details.item(w_details.count()).getAspect();
                minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
            } 
            else {
                // bottom of the previous group footer + C_Min_Height_Section
                //
                cReportAspect w_aspect = m_report.getGroupsFooters().item(index - 1).getAspect(index - 1).Aspect;
                minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
            }

            if (!isForSectionLine) {
                minBottom = pGetMinBottomWithSecLn(rptSec.getSectionLines(), minBottom);
            }
            maxBottom = m_picReport.Height;

            return rptSec;
        }

        private cReportSection pMoveFooter(
            String sKeySection, 
            int minBottom, 
            int maxBottom, 
            bool isForSectionLine) 
        {

            int index = 0;
            cReportSection rptSec = null;

            rptSec = m_report.getFooters().item(sKeySection);

            index = rptSec.getrealIndex();

            //-----------
            // MinBottom
            //-----------
            if (index == 1) {
                
                // if there are groups
                //
                if (m_report.getGroupsFooters().count() > 0) {
                    
                    // the bottom of the last group footer
                    //
                    cIReportGroupSections w_groupsFooters = m_report.getGroupsFooters();
                    cReportAspect w_aspect = w_groupsFooters.item(w_groupsFooters.count()).getAspect();
                    minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
                } 
                else {
                    // bottom of the last detail
                    //
                    cReportSections w_details = m_report.getDetails();
                    cReportAspect w_aspect = w_details.item(w_aspect.count()).getAspect();
                    minBottom = w_aspect.getHeight() + w_aspect.getTop() + C_MIN_HEIGHT_SECTION;
                }
            } 
            else {
                // bottom of the previous footer
                //
                cReportAspect w_aspect = m_report.getFooters().item(index - 1).getAspect(index - 1).Aspect;
                minBottom = w_aspect.getHeight() + w_aspect.getTop() - m_offSet + C_MIN_HEIGHT_SECTION;
            }

            if (!isForSectionLine) {
                minBottom = pGetMinBottomWithSecLn(rptSec.getSectionLines(), minBottom);
            }

            maxBottom = m_picReport.Height;

            return rptSec;
        }

        private int pGetMinBottomWithSecLn(cReportSectionLines secLns, int minBottom) { 
            int i = 0;

            for (i = 1; i <= secLns.count() - 1; i++) {
                minBottom = minBottom + secLns(i).Aspect.Height;
            }

            return minBottom;
        }

        private void pGetOffSet(float realPageHeight, float rtnPageHeight) {
            cReportSection sec = null;

            rtnPageHeight = 0;

            for (int _i = 0; _i < m_report.getHeaders().size(); _i++) {
                Sec = m_Report.Headers.getItem(_i);
                rtnPageHeight = rtnPageHeight + sec.getAspect().getHeight();
            }

            for (int _i = 0; _i < m_report.getGroupsHeaders().size(); _i++) {
                Sec = m_Report.GroupsHeaders.getItem(_i);
                rtnPageHeight = rtnPageHeight + sec.getAspect().getHeight();
            }

            for (int _i = 0; _i < m_report.getDetails().size(); _i++) {
                Sec = m_Report.Details.getItem(_i);
                rtnPageHeight = rtnPageHeight + sec.getAspect().getHeight();
            }

            for (int _i = 0; _i < m_report.getGroupsFooters().size(); _i++) {
                Sec = m_Report.GroupsFooters.getItem(_i);
                rtnPageHeight = rtnPageHeight + sec.getAspect().getHeight();
            }

            for (int _i = 0; _i < m_report.getFooters().size(); _i++) {
                Sec = m_Report.Footers.getItem(_i);
                rtnPageHeight = rtnPageHeight + sec.getAspect().getHeight();
            }

            m_offSet = realPageHeight - rtnPageHeight;

            if (m_offSet < 0) { m_offSet = 0; }
        }

        private void pRefreshOffSetInPaintObjs() {
            cReportSection sec = null;
            cReportSectionLine secLines = null;
            cReportControl ctl = null;

            cReportPaintObjects w_paintSections = m_paint.getPaintSections();
                for (int _i = 0; _i < m_report.getFooters().size(); _i++) {
                    Sec = m_Report.Footers.getItem(_i);
                    w_paintSections.item(sec.getKeyPaint()).getAspect().setOffset(m_offSet);
                    for (int _j = 0; _j < sec.getSectionLines().size(); _j++) {
                        SecLines = Sec.SectionLines.getItem(_j);
                        if (secLines.getKeyPaint() != "") {
                            w_paintSections.item(secLines.getKeyPaint()).getAspect().setOffset(m_offSet);
                        }
                        for (int _k = 0; _k < secLines.getControls().size(); _k++) {
                            Ctl = SecLines.Controls.getItem(_k);
                            CSReportPaint.cReportPaintObject po;
                            po = m_paint.getPaintObjects().item(ctl.getKeyPaint());
                            po.Aspect.OffSet = m_offSet;
                        }
                    }
                }
        }

        // if the click was over a control which is not part of the
        // selected controls collection we clear the selected collection
        // and add the control which was clicked to the selected collection
        //
        private bool pSetSelectForRightBttn() {
            int i = 0;
            for (i = 1; i <= m_vSelectedKeys.Length; i++) {
                if (m_vSelectedKeys[i] == m_keyObj) { return; }
            }

            G.redim(m_vSelectedKeys, 1);
            m_vSelectedKeys[1] = m_keyObj;

            return true;
        }

        private void pValidateSectionAspect() {
            cReportSection sec = null;
            int top = 0;
            int i = 0;

            for (int _i = 0; _i < m_report.getHeaders().size(); _i++) {
                Sec = m_Report.Headers.getItem(_i);
                top = pValidateSectionAspecAux(top, sec);
            }

            for (int _i = 0; _i < m_report.getGroupsHeaders().size(); _i++) {
                Sec = m_Report.GroupsHeaders.getItem(_i);
                top = pValidateSectionAspecAux(top, sec);
            }

            for (int _i = 0; _i < m_report.getDetails().size(); _i++) {
                Sec = m_Report.Details.getItem(_i);
                top = pValidateSectionAspecAux(top, sec);
            }

            for (int _i = 0; _i < m_report.getGroupsFooters().size(); _i++) {
                Sec = m_Report.GroupsFooters.getItem(_i);
                top = pValidateSectionAspecAux(top, sec);
            }

            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            top = mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                    w_paperInfo.getPaperSize(), 
                                                    w_paperInfo.getOrientation()).height;

            for (i = m_report.getFooters().count(); i <= 1; i--) {
                sec = m_report.getFooters(i);
                top = top - sec.getAspect().getHeight();
                pValidateSectionAspecAux(top, sec);
            }
        }

        private int pValidateSectionAspecAux(int top, cReportSection sec) {
            cReportSectionLine secLn = null;
            int topLn = 0;
            int i = 0;
            int secLnHeight = 0;
            int width = 0;

            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            width = mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                    w_paperInfo.getPaperSize(), 
                                                    w_paperInfo.getOrientation()).width;
            topLn = top;

            for (i = 1; i <= sec.getSectionLines().count() - 1; i++) {
                secLn = sec.getSectionLines(i);
                cReportAspect w_aspect = secLn.getAspect();
                w_aspect.setTop(topLn);
                w_aspect.setWidth(width);
                if (w_aspect.getHeight() < C_MIN_HEIGHT_SECTION) {
                    w_aspect.setHeight(C_MIN_HEIGHT_SECTION);
                }
                topLn = topLn + w_aspect.getHeight();
                secLnHeight = secLnHeight + w_aspect.getHeight();
            }

            cReportSectionLines w_sectionLines = sec.getSectionLines();
            secLn = w_sectionLines.item(w_sectionLines.count());

            cReportAspect w_aspect = secLn.getAspect();
            w_aspect.setTop(topLn);
            w_aspect.setHeight(sec.getAspect().getHeight() - secLnHeight);
            if (w_aspect.getHeight() < C_MIN_HEIGHT_SECTION) {
                w_aspect.setHeight(C_MIN_HEIGHT_SECTION);
            }
            secLnHeight = secLnHeight + w_aspect.getHeight();

            cReportAspect w_aspect = sec.getAspect();
            w_aspect.setHeight(secLnHeight);
            if (w_aspect.getHeight() < C_MIN_HEIGHT_SECTION) {
                w_aspect.setHeight(C_MIN_HEIGHT_SECTION);
            }
            w_aspect.setWidth(width);
            w_aspect.setTop(top);
            topLn = top;
            top = top + w_aspect.getHeight();

            pChangeTopSection(sec, 0, false, false);
            return top;
        }

        public void showControls() {
            try {

                Application.DoEvents();

                m_fControls = mPublic.getCtrlBox(this);
                mPublic.clearCtrlBox(this);
                cReportControl ctrl = null;
                m_fControls.addCtrls(m_report);
                m_fControls.Show(vbModeless, fMain);

            } catch (Exception ex) {
                cError.mngError(ex, "ShowControls", C_MODULE, "");
            }
        }

        public void showControlsTree() {
            try {

                Application.DoEvents();

                m_fTreeCtrls = mPublic.getCtrlTreeBox(this);
                mPublic.clearCtrlTreeBox(this);

                cReportControl ctrl = null;
                m_fTreeCtrls.addCtrls(m_report);
                m_fTreeCtrls.Show(vbModeless);

            } catch (Exception ex) {
                cError.mngError(ex, "ShowControlsTree", C_MODULE, "");
            }
        }

        private void pSetInitDir() {
            if (mPublic.gbFirstOpen) {
                mPublic.gbFirstOpen = false;
                fMain.cmDialog.InitDir = mPublic.gWorkFolder;
            }
        }

        private void form_Load() {
            G.redim(m_vSelectedKeys, 0);
            G.redim(m_vCopyKeys, 0);
            m_copyControls = false;
            m_copyControlsFromOtherReport = false;
            m_typeGrid = CSReportPaint.CSEGRIDPOINTS;
            m_keyboardMoveStep = 50;
        }

        private void form_QueryUnload(int cancel, int unloadMode) {
            cancel = !saveChanges();
            if (cancel) { mPublic.setDocActive(this); }
        }

        private void form_Unload(int cancel) {
            if (fMain.getReportCopySource() == this) {
                fMain.setReportCopySource(null);
            }
            if (fSearch.fSearch.getFReport() == this) {
                fSearch.fSearch.setFReport(null);
            }
            m_report = null;
            m_paint = null;
            m_fToolBox = null;
            m_fControls = null;
            m_fTreeCtrls = null;
            m_fConnectsAux = null;
            m_fProperties = null;
            m_fFormula = null;
            m_fGroup = null;
            Unload(m_fProgress);
            m_fProgress = null;
            mPublic.setDocInacActive(this);
            G.redim(m_vSelectedKeys, 0);
            G.redim(m_vCopyKeys, 0);
        }

        public void init() {
            m_showingProperties = false;

            cReportLaunchInfo oLaunchInfo = null;
            m_report = new cReport();
            // TODO: event handler for
            //
            / *
                        m_Report_Done();
                        m_Report_Progress(task, page, currRecord, recordCount, cancel,);
                        m_Report_FindFileAccess(answer, commDialog, file,);
            * /
            oLaunchInfo = new cReportLaunchInfo();

            m_report.getPaperInfo().setPaperSize(fMain.getPaperSize());
            m_report.getPaperInfo().setOrientation(fMain.getOrientation());

            oLaunchInfo.setPrinter(mPrintAPI.getcPrinterFromDefaultPrinter());
            oLaunchInfo.setObjPaint(new CSReportPaint.cReportPrint());
            if (!m_report.init(oLaunchInfo)) { return; }

            CSKernelFile.cFile file = new CSKernelFile.cFile(); 
            m_report.setPathDefault(file.GetPath(App.Path));

            m_picReport.Top = C_TOPBODY;
            m_picRule.Left = 0;
            m_picReport.Left = pGetLeftBody();

            m_keyMoving = "";
            m_keySizing = "";
            m_keyObj = "";
            m_keyFocus = "";
            m_nextNameCtrl = 0;

            Title = "CSReportEditor";

            m_paint = new CSReportPaint.cReportPaint();

            Rectangle tR = null;
            cReportPaperInfo w_paperInfo = m_report.getPaperInfo();
            tR = mPrintAPI.getRectFromPaperSize(m_report.getPaperInfo(), 
                                                w_paperInfo.getPaperSize(), 
                                                w_paperInfo.getOrientation());
            mPublic.createStandarSections(m_report, tR);
            m_paint.setGridHeight(pSetSizePics(tR.height));
            m_paint.initGrid(m_picReport, m_typeGrid);

            paintStandarSections();

            m_dataHasChanged = false;
        }

        private void pUpdateFormulas(String currentName, String newName) {
            int i = 0;
            cReportControl rptCtrl = null;

            for (i = 1; i <= m_report.getControls().count(); i++) {

                rptCtrl = m_report.getControls().item(i);

                cReportFormula w_formulaHide = rptCtrl.getFormulaHide();
                if (w_formulaHide.getText() != "") {
                    if (w_formulaHide.getText().indexOf(currentName, 1)) {
                        w_formulaHide.setText(pReplaceInFormula(w_formulaHide.getText(), currentName, newName));
                    }
                }

                cReportFormula w_formulaValue = rptCtrl.getFormulaValue();
                if (w_formulaValue.getText() != "") {
                    if (w_formulaValue.getText().indexOf(currentName, 1)) {
                        w_formulaValue.setText(pReplaceInFormula(w_formulaValue.getText(), currentName, newName));
                    }
                }
            }
        }

        private String pReplaceInFormula(String formulaText, String currentName, String newName) {
            String _rtn = "";

            // if it isn't an internal function we give the user
            // a chance to cancel the changes
            //
            if (formulaText.Substring(0, 1).Trim() != "_") {
                fFormulaReplace fReplace = null;
                fReplace = new fFormulaReplace();
                fReplace.ctxCurrFormula.Text = formulaText;
                fReplace.ctxNewFormula.Text = formulaText.replace(currentName, newName);
                fReplace.Show(vbModal);
                if (fReplace.getOk()) {
                    _rtn = fReplace.ctxNewFormula.Text;
                } 
                else {
                    _rtn = formulaText;
                }
                Unload(fReplace);

            } 
            else {

                _rtn = formulaText.replace(currentName, newName);
            }
            return _rtn;
        }

        private void form_Activate() {
            mPublic.setDocActive(this);
            if (fToolbox.fToolbox.getLoaded()) {
                if (!mPublic.getToolBox(this) == null) { showToolBox(); }
            }
            if (fControls.fToolbox.getLoaded()) {
                if (!mPublic.getCtrlBox(this) == null) { showControls(); }
            }
        }

        private void form_Deactivate() {
            mPublic.setDocInacActive(this);
            mPublic.clearToolBox(this);
        }

        ////////////////////////////////
        //  Codigo estandar de errores
        //  On Error GoTo ControlError
        //
        //  GoTo ExitProc
        //ControlError:
        //  MngError err,"", C_Module, ""
        //  If Err.Number Then Resume ExitProc
        //ExitProc:
        //  On Error Resume Next
      
        */
    }

    enum csAskEditResult {
        CSASKRSLTYES = 1,
        CSASKRSLTNO = 2,
        CSASKRSLTCANCEL = 3
    }
}
